diff options
author | Loïc Hoguin <[email protected]> | 2019-09-24 19:18:35 +0200 |
---|---|---|
committer | Loïc Hoguin <[email protected]> | 2019-09-24 19:28:48 +0200 |
commit | d9a970be90d0105af215531d74809878f9c21338 (patch) | |
tree | eb891e8c9373dee9f7353a9920aaf25f6b2f2570 /src/gun.erl | |
parent | a18ca0ae8ff76594c7b629f4340adab0a30954c4 (diff) | |
download | gun-d9a970be90d0105af215531d74809878f9c21338.tar.gz gun-d9a970be90d0105af215531d74809878f9c21338.tar.bz2 gun-d9a970be90d0105af215531d74809878f9c21338.zip |
Add auto-ping to Websocket and a silence_pings option
The auto-ping will at regular interval send a ping frame.
The silence_pings option defaults to true. It can be set
to false when the user needs to receive ping/pong frames.
Diffstat (limited to 'src/gun.erl')
-rw-r--r-- | src/gun.erl | 15 |
1 files changed, 9 insertions, 6 deletions
diff --git a/src/gun.erl b/src/gun.erl index ab26dbf..12f4319 100644 --- a/src/gun.erl +++ b/src/gun.erl @@ -222,12 +222,13 @@ }. -export_type([socks_opts/0]). -%% @todo keepalive -type ws_opts() :: #{ closing_timeout => timeout(), compress => boolean(), flow => pos_integer(), - protocols => [{binary(), module()}] + keepalive => timeout(), + protocols => [{binary(), module()}], + silence_pings => boolean() }. -export_type([ws_opts/0]). @@ -602,7 +603,7 @@ connect(ServerPid, Destination, Headers, ReqOpts) -> | {trailers, resp_headers()} | {push, reference(), binary(), binary(), resp_headers()} | {upgrade, [binary()], resp_headers()} - | {ws, ws_frame()} %% @todo Excluding ping/pong, for now. + | {ws, ws_frame()} | {error, {stream_error | connection_error | down, any()} | timeout}. -spec await(pid(), reference()) -> await_result(). @@ -1225,9 +1226,11 @@ handle_common_connected_no_input(info, {Error, Socket, Reason}, _, %% We should have a timeout function in protocols that deal with %% received timeouts. Currently the timeout messages are ignored. handle_common_connected_no_input(info, keepalive, _, - State=#state{protocol=Protocol, protocol_state=ProtoState}) -> - ProtoState2 = Protocol:keepalive(ProtoState), - {keep_state, keepalive_timeout(State#state{protocol_state=ProtoState2})}; + State=#state{protocol=Protocol, protocol_state=ProtoState0, + event_handler=EvHandler, event_handler_state=EvHandlerState0}) -> + {ProtoState, EvHandlerState} = Protocol:keepalive(ProtoState0, EvHandler, EvHandlerState0), + {keep_state, keepalive_timeout(State#state{ + protocol_state=ProtoState, event_handler_state=EvHandlerState})}; handle_common_connected_no_input(cast, {update_flow, ReplyTo, StreamRef, Flow}, _, State0=#state{protocol=Protocol, protocol_state=ProtoState}) -> Commands = Protocol:update_flow(ProtoState, ReplyTo, StreamRef, Flow), |