1
%% Copyright (c) 2011, Loïc Hoguin <essen@dev-extend.eu>
3
%% Permission to use, copy, modify, and/or distribute this software for any
4
%% purpose with or without fee is hereby granted, provided that the above
5
%% copyright notice and this permission notice appear in all copies.
7
%% THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8
%% WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9
%% MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
10
%% ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11
%% WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
12
%% ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
13
%% OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15
%% @doc Handler for HTTP WebSocket requests.
17
%% WebSocket handlers must implement four callbacks: <em>websocket_init/3</em>,
18
%% <em>websocket_handle/3</em>, <em>websocket_info/3</em> and
19
%% <em>websocket_terminate/3</em>. These callbacks will only be called if the
20
%% connection is upgraded to WebSocket in the HTTP handler's <em>init/3</em>
21
%% callback. They are then called in that order, although
22
%% <em>websocket_handle/3</em> will be called for each packet received,
23
%% and <em>websocket_info</em> for each message received.
25
%% <em>websocket_init/3</em> is meant for initialization. It receives
26
%% information about the transport and protocol used, along with the handler
27
%% options from the dispatch list. You can define a request-wide state here.
28
%% If you are going to want to compact the request, you should probably do it
31
%% <em>websocket_handle/3</em> receives the data from the socket. It can reply
32
%% something, do nothing or close the connection.
34
%% <em>websocket_info/3</em> receives messages sent to the process. It has
35
%% the same reply format as <em>websocket_handle/3</em> described above. Note
36
%% that unlike in a <em>gen_server</em>, when <em>websocket_info/3</em>
37
%% replies something, it is always to the socket, not to the process that
38
%% originated the message.
40
%% <em>websocket_terminate/3</em> is meant for cleaning up. It also receives
41
%% the request and the state previously defined, along with a reason for
44
%% All of <em>websocket_init/3</em>, <em>websocket_handle/3</em> and
45
%% <em>websocket_info/3</em> can decide to hibernate the process by adding
46
%% an extra element to the returned tuple, containing the atom
47
%% <em>hibernate</em>. Doing so helps save memory and improve CPU usage.
48
-module(cowboy_http_websocket_handler).
50
-export([behaviour_info/1]).
53
-spec behaviour_info(_)
54
-> undefined | [{websocket_handle, 3} | {websocket_info, 3}
55
| {websocket_init, 3} | {websocket_terminate, 3}, ...].
56
behaviour_info(callbacks) ->
57
[{websocket_init, 3}, {websocket_handle, 3},
58
{websocket_info, 3}, {websocket_terminate, 3}];
59
behaviour_info(_Other) ->