aboutsummaryrefslogtreecommitdiffstats
path: root/test/handlers/ws_active_commands_h.erl
blob: 4cdb3b12f17cca6789f299b043d868e7a66dda51 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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}.