Skip to content

Commit

Permalink
WebSocket: send ping and pong payloads
Browse files Browse the repository at this point in the history
Since anmonteiro/httpun-ws#36 was fixed
recently, websocket/af supports sending ping and pong payloads, as
required by the WebSocket spec. This commit has dream-httpaf using that
support.

See also #181.
  • Loading branch information
aantron committed Nov 10, 2023
1 parent 8cb51f1 commit aaabf4d
Showing 1 changed file with 14 additions and 12 deletions.
26 changes: 14 additions & 12 deletions src/http/shared/websocket.ml
Original file line number Diff line number Diff line change
Expand Up @@ -204,32 +204,34 @@ let websocket_handler stream socket =
outgoing_loop ()
end)
~flush:(fun () -> flush ~close outgoing_loop)
~ping:(fun _buffer _offset length ->
~ping:(fun buffer offset length ->
if length > 125 then
raise (Failure "Ping payload cannot exceed 125 bytes");
(* See https://github.com/anmonteiro/websocketaf/issues/36. *)
(* if length > 0 then
websocket_log.warning (fun log ->
log "Ping with non-empty payload not yet supported"); *)
if !closed then
close !close_code
else begin
Websocketaf.Wsd.send_ping socket;
if length = 0 then
Websocketaf.Wsd.send_ping socket
else
Websocketaf.Wsd.send_ping
~application_data:{Faraday.buffer; off = offset; len = length}
socket;
outgoing_loop ()
end)
~pong:(fun _buffer _offset length ->
~pong:(fun buffer offset length ->
(* TODO Is there any way for the peer to send a ping payload with more
than 125 bytes, forcing a too-large pong and an exception? *)
if length > 125 then
raise (Failure "Pong payload cannot exceed 125 bytes");
(* See https://github.com/anmonteiro/websocketaf/issues/36. *)
(* if length > 0 then
websocket_log.warning (fun log ->
log "Pong with non-empty payload not yet supported"); *)
if !closed then
close !close_code
else begin
Websocketaf.Wsd.send_pong socket;
if length = 0 then
Websocketaf.Wsd.send_pong socket
else
Websocketaf.Wsd.send_pong
~application_data:{Faraday.buffer; off = offset; len = length}
socket;
outgoing_loop ()
end)
~close
Expand Down

0 comments on commit aaabf4d

Please sign in to comment.