forked from ninenines/cowboy
/
simple_ws_server.erl
58 lines (44 loc) · 1.22 KB
/
simple_ws_server.erl
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
-module(simple_ws_server).
-behaviour(cowboy_http_handler).
-behaviour(cowboy_http_websocket_handler).
-define(SEND_IDLE,1).
-export([start/0]).
-export([init/3, handle/2, terminate/2]).
-export([websocket_init/3, websocket_handle/3, websocket_info/3, websocket_terminate/3]).
dispatch() ->
[
{'_', [
{'_', simple_ws_server, []}
]
}
].
init(_Any, _Req, _Opts) ->
{upgrade, protocol, cowboy_http_websocket}.
handle(_Req, _State) ->
exit(badarg).
terminate(_Req, _State) ->
exit(badarg).
-ifdef(SEND_IDLE).
start_timer() ->
erlang:start_timer(25000, self(), idlemsg).
-else.
start_timer() ->
ok.
-endif.
websocket_init(_, Req, _) ->
start_timer(),
{ok,cowboy_http_req:compact(Req),undefined}.
websocket_handle(_, Req, State) ->
{ok,Req,State}.
websocket_info({timeout,_,_}, Req, State) ->
start_timer(),
{reply, {text, <<"{}">>}, Req, State}.
websocket_terminate(_Reason, _Req, _State) ->
ok.
start() ->
{ok,_}=cowboy:start_listener(
simple_ws_listener,
1024,
cowboy_tcp_transport, [{port, 8003}],
cowboy_http_protocol, [{dispatch, dispatch()},
{timeout, 60000}]).