2016-08-15 19:21:38 +02:00
|
|
|
%% This module returns a different value in websocket_init/1 depending on the query string.
|
|
|
|
|
|
|
|
-module(ws_init_h).
|
|
|
|
-behavior(cowboy_websocket).
|
|
|
|
|
|
|
|
-export([init/2]).
|
|
|
|
-export([websocket_init/1]).
|
|
|
|
-export([websocket_handle/2]).
|
|
|
|
-export([websocket_info/2]).
|
|
|
|
|
|
|
|
init(Req, _) ->
|
|
|
|
State = binary_to_atom(cowboy_req:qs(Req), latin1),
|
|
|
|
{cowboy_websocket, Req, State}.
|
|
|
|
|
|
|
|
%% Sleep to make sure the HTTP response was sent.
|
|
|
|
websocket_init(State) ->
|
|
|
|
timer:sleep(100),
|
|
|
|
do_websocket_init(State).
|
|
|
|
|
|
|
|
do_websocket_init(State=ok) ->
|
2019-10-06 16:51:27 +02:00
|
|
|
{[], State};
|
2016-08-15 19:21:38 +02:00
|
|
|
do_websocket_init(State=ok_hibernate) ->
|
2019-10-06 16:51:27 +02:00
|
|
|
{[], State, hibernate};
|
2016-08-15 19:21:38 +02:00
|
|
|
do_websocket_init(State=reply) ->
|
2019-10-06 16:51:27 +02:00
|
|
|
{[{text, "Hello"}], State};
|
2016-08-15 19:21:38 +02:00
|
|
|
do_websocket_init(State=reply_hibernate) ->
|
2019-10-06 16:51:27 +02:00
|
|
|
{[{text, "Hello"}], State, hibernate};
|
2016-08-15 19:21:38 +02:00
|
|
|
do_websocket_init(State=reply_close) ->
|
2019-10-06 16:51:27 +02:00
|
|
|
{[close], State};
|
2016-08-15 19:21:38 +02:00
|
|
|
do_websocket_init(State=reply_close_hibernate) ->
|
2019-10-06 16:51:27 +02:00
|
|
|
{[close], State, hibernate};
|
2016-08-15 19:21:38 +02:00
|
|
|
do_websocket_init(State=reply_many) ->
|
2019-10-06 16:51:27 +02:00
|
|
|
{[{text, "Hello"}, {binary, "World"}], State};
|
2016-08-15 19:21:38 +02:00
|
|
|
do_websocket_init(State=reply_many_hibernate) ->
|
2019-10-06 16:51:27 +02:00
|
|
|
{[{text, "Hello"}, {binary, "World"}], State, hibernate};
|
2016-08-15 19:21:38 +02:00
|
|
|
do_websocket_init(State=reply_many_close) ->
|
2019-10-06 16:51:27 +02:00
|
|
|
{[{text, "Hello"}, close], State};
|
2016-08-15 19:21:38 +02:00
|
|
|
do_websocket_init(State=reply_many_close_hibernate) ->
|
2024-01-08 11:44:34 +01:00
|
|
|
{[{text, "Hello"}, close], State, hibernate};
|
|
|
|
do_websocket_init(State=reply_trap_exit) ->
|
|
|
|
Text = "trap_exit: " ++ atom_to_list(element(2, process_info(self(), trap_exit))),
|
|
|
|
{[{text, Text}, close], State, hibernate}.
|
2016-08-15 19:21:38 +02:00
|
|
|
|
|
|
|
websocket_handle(_, State) ->
|
2019-10-06 16:51:27 +02:00
|
|
|
{[], State}.
|
2016-08-15 19:21:38 +02:00
|
|
|
|
|
|
|
websocket_info(_, State) ->
|
2019-10-06 16:51:27 +02:00
|
|
|
{[], State}.
|