aboutsummaryrefslogtreecommitdiffstats
path: root/test/handlers/ws_info_commands_h.erl
blob: d596473376bf8f1ed9955e59d2908ede73f0501b (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
31
32
%% This module takes commands from the x-commands header
%% and returns them in the websocket_info/2 callback.
%% This callback is triggered via a message.

-module(ws_info_commands_h).
-behavior(cowboy_websocket).

-export([init/2]).
-export([websocket_init/1]).
-export([websocket_handle/2]).
-export([websocket_info/2]).

init(Req=#{pid := Pid}, RunOrHibernate) ->
	Commands0 = cowboy_req:header(<<"x-commands">>, Req),
	Commands = binary_to_term(base64:decode(Commands0)),
	case Commands of
		bad -> ct_helper_error_h:ignore(Pid, cowboy_websocket, handler_call, 6);
		_ -> ok
	end,
	{cowboy_websocket, Req, {Commands, RunOrHibernate}}.

websocket_init(State) ->
	self() ! shoot,
	{[], State}.

websocket_handle(_, State) ->
	{[], State}.

websocket_info(_, State={Commands, run}) ->
	{Commands, State};
websocket_info(_, State={Commands, hibernate}) ->
	{Commands, State, hibernate}.