| 1 |
%% This module sets options based on the frame received. |
| 2 |
|
| 3 |
-module(ws_set_options_commands_h). |
| 4 |
-behavior(cowboy_websocket). |
| 5 |
|
| 6 |
-export([init/2]). |
| 7 |
-export([websocket_handle/2]). |
| 8 |
-export([websocket_info/2]). |
| 9 |
|
| 10 |
init(Req, Opts) -> |
| 11 |
DataDelivery = maps:get(data_delivery, Opts, stream_handlers), |
| 12 |
{cowboy_websocket, Req, maps:get(run_or_hibernate, Opts), |
| 13 |
#{idle_timeout => infinity, data_delivery => DataDelivery}}. |
| 14 |
|
| 15 |
%% Set the idle_timeout option dynamically. |
| 16 |
websocket_handle({text, <<"idle_timeout_short">>}, State=run) -> |
| 17 |
{[{set_options, #{idle_timeout => 500}}], State}; |
| 18 |
websocket_handle({text, <<"idle_timeout_short">>}, State=hibernate) -> |
| 19 |
{[{set_options, #{idle_timeout => 500}}], State, hibernate}; |
| 20 |
%% Set the max_frame_size option dynamically. |
| 21 |
websocket_handle({text, <<"max_frame_size_small">>}, State=run) -> |
| 22 |
{[{set_options, #{max_frame_size => 1000}}], State}; |
| 23 |
websocket_handle({text, <<"max_frame_size_small">>}, State=hibernate) -> |
| 24 |
{[{set_options, #{max_frame_size => 1000}}], State, hibernate}; |
| 25 |
%% We just echo binary frames. |
| 26 |
websocket_handle(Frame={binary, _}, State=run) -> |
| 27 |
{[Frame], State}; |
| 28 |
websocket_handle(Frame={binary, _}, State=hibernate) -> |
| 29 |
{[Frame], State, hibernate}. |
| 30 |
|
| 31 |
websocket_info(_Info, State) -> |
| 32 |
{[], State}. |