From f810d8dd6496da713e7c70a5e146120de3695774 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Fri, 21 Sep 2018 14:04:20 +0200 Subject: Add the {active, boolean()} Websocket command This command is currently not documented. It allows disabling the reading of incoming data from the socket, and can be used as a poor man's flow control. --- test/handlers/ws_active_commands_h.erl | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 test/handlers/ws_active_commands_h.erl (limited to 'test/handlers') diff --git a/test/handlers/ws_active_commands_h.erl b/test/handlers/ws_active_commands_h.erl new file mode 100644 index 0000000..4cdb3b1 --- /dev/null +++ b/test/handlers/ws_active_commands_h.erl @@ -0,0 +1,30 @@ +%% This module takes commands from the x-commands header +%% and returns them in the websocket_init/1 callback. + +-module(ws_active_commands_h). +-behavior(cowboy_websocket). + +-export([init/2]). +-export([websocket_init/1]). +-export([websocket_handle/2]). +-export([websocket_info/2]). + +init(Req, RunOrHibernate) -> + {cowboy_websocket, Req, RunOrHibernate}. + +websocket_init(State=run) -> + erlang:send_after(1500, self(), active_true), + {[{active, false}], State}; +websocket_init(State=hibernate) -> + erlang:send_after(1500, self(), active_true), + {[{active, false}], State, hibernate}. + +websocket_handle(Frame, State=run) -> + {[Frame], State}; +websocket_handle(Frame, State=hibernate) -> + {[Frame], State, hibernate}. + +websocket_info(active_true, State=run) -> + {[{active, true}], State}; +websocket_info(active_true, State=hibernate) -> + {[{active, true}], State, hibernate}. -- cgit v1.2.3