| 1 |
%% This module starts with active mode disabled |
| 2 |
%% and enables it again once a timeout is triggered. |
| 3 |
|
| 4 |
-module(ws_active_commands_h). |
| 5 |
-behavior(cowboy_websocket). |
| 6 |
|
| 7 |
-export([init/2]). |
| 8 |
-export([websocket_init/1]). |
| 9 |
-export([websocket_handle/2]). |
| 10 |
-export([websocket_info/2]). |
| 11 |
|
| 12 |
init(Req, Opts) -> |
| 13 |
{cowboy_websocket, Req, maps:get(run_or_hibernate, Opts), Opts}. |
| 14 |
|
| 15 |
websocket_init(State=run) -> |
| 16 |
erlang:send_after(1500, self(), active_true), |
| 17 |
{[{active, false}], State}; |
| 18 |
websocket_init(State=hibernate) -> |
| 19 |
erlang:send_after(1500, self(), active_true), |
| 20 |
{[{active, false}], State, hibernate}. |
| 21 |
|
| 22 |
websocket_handle(Frame, State=run) -> |
| 23 |
{[Frame], State}; |
| 24 |
websocket_handle(Frame, State=hibernate) -> |
| 25 |
{[Frame], State, hibernate}. |
| 26 |
|
| 27 |
websocket_info(active_true, State=run) -> |
| 28 |
{[{active, true}], State}; |
| 29 |
websocket_info(active_true, State=hibernate) -> |
| 30 |
{[{active, true}], State, hibernate}. |