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_ws_h.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_ws_h.erl')
-rw-r--r-- | src/gun_ws_h.erl | 11 |
1 files changed, 8 insertions, 3 deletions
diff --git a/src/gun_ws_h.erl b/src/gun_ws_h.erl index 4859532..88f923f 100644 --- a/src/gun_ws_h.erl +++ b/src/gun_ws_h.erl @@ -20,11 +20,13 @@ -record(state, { reply_to :: pid(), stream_ref :: reference(), - frag_buffer = <<>> :: binary() + frag_buffer = <<>> :: binary(), + silence_pings :: boolean() }). -init(ReplyTo, StreamRef, _, _) -> - {ok, #state{reply_to=ReplyTo, stream_ref=StreamRef}}. +init(ReplyTo, StreamRef, _, Opts) -> + {ok, #state{reply_to=ReplyTo, stream_ref=StreamRef, + silence_pings=maps:get(silence_pings, Opts, true)}}. handle({fragment, nofin, _, Payload}, State=#state{frag_buffer=SoFar}) -> @@ -33,6 +35,9 @@ handle({fragment, fin, Type, Payload}, State=#state{reply_to=ReplyTo, stream_ref=StreamRef, frag_buffer=SoFar}) -> ReplyTo ! {gun_ws, self(), StreamRef, {Type, << SoFar/binary, Payload/binary >>}}, {ok, 1, State#state{frag_buffer= <<>>}}; +handle(Frame, State=#state{silence_pings=true}) when Frame =:= ping; Frame =:= pong; + element(1, Frame) =:= ping; element(1, Frame) =:= pong -> + {ok, 0, State}; handle(Frame, State=#state{reply_to=ReplyTo, stream_ref=StreamRef}) -> ReplyTo ! {gun_ws, self(), StreamRef, Frame}, {ok, 1, State}. |