Debian Codegraph @

test/handlers/ws_info_commands_h.erl

raw
1 %% This module takes commands from the x-commands header
2 %% and returns them in the websocket_info/2 callback.
3 %% This callback is triggered via a message.
4
5 -module(ws_info_commands_h).
6 -behavior(cowboy_websocket).
7
8 -export([init/2]).
9 -export([websocket_init/1]).
10 -export([websocket_handle/2]).
11 -export([websocket_info/2]).
12
13 init(Req, Opts) ->
14 Commands0 = cowboy_req:header(<<"x-commands">>, Req),
15 Commands = binary_to_term(base64:decode(Commands0)),
16 case Commands of
17 bad ->
18 Pid = case Req of
19 #{version := 'HTTP/2'} -> self();
20 #{pid := Pid0} -> Pid0
21 end,
22 ct_helper_error_h:ignore(Pid, cowboy_websocket, handler_call, 6);
23 _ ->
24 ok
25 end,
26 {cowboy_websocket, Req, {Commands, maps:get(run_or_hibernate, Opts)}, Opts}.
27
28 websocket_init(State) ->
29 self() ! shoot,
30 {[], State}.
31
32 websocket_handle(_, State) ->
33 {[], State}.
34
35 websocket_info(_, State={Commands, run}) ->
36 {Commands, State};
37 websocket_info(_, State={Commands, hibernate}) ->
38 {Commands, State, hibernate}.