1
1
# elli_websocket
2
2
3
- * A WebSocket handler for [ Elli ] ( https://github.com/ elli-lib/elli ) *
3
+ * A WebSocket handler for [ elli] [ ] *
4
4
5
5
[ ![ Erlang] [ erlang badge ]] [ erlang downloads ]
6
6
[ ![ Travis CI] [ travis badge ]] [ travis builds ]
7
7
[ ![ Coverage Status] [ coveralls badge ]] [ coveralls link ]
8
8
[ ![ Apache License] [ license badge ]] ( LICENSE )
9
9
10
+ [ elli ] : https://github.com/elli-lib/elli
10
11
[ erlang badge ] : https://img.shields.io/badge/erlang-%E2%89%A518.0-red.svg
11
12
[ erlang downloads ] : http://www.erlang.org/downloads
12
13
[ travis builds ] : https://travis-ci.org/elli-lib/elli_websocket
18
19
19
20
## Installation
20
21
21
- You can add ` elli_websocket ` to your application by adding it as a dependency to your elli
22
- application.
22
+ You can add ` elli_websocket ` to your application by adding it as a dependency to
23
+ your [ elli ] [ ] application.
23
24
24
25
``` erlang
25
26
{deps , [
26
- {elli_websocket , " .*" , {git , " git://github.com/elli-lib/elli_websocket.git" , {branch , " master" }}}
27
+ {elli , " 2.0.1" },
28
+ {elli_websocket , " 0.1.0" }
27
29
]}.
28
30
```
29
31
30
32
31
- ## Callback Module
33
+ ## Examples
34
+
35
+ ### Callback Module
32
36
33
37
See [ elli_example_websocket.erl] ( ./src/elli_example_websocket.erl ) for details.
34
38
35
39
``` erlang
36
40
-module (elli_echo_websocket_handler ).
37
- -export ([websocket_init /1 , websocket_handle /3 , websocket_info /3 , websocket_handle_event /3 ]).
41
+
42
+ -export ([websocket_init /1 ,
43
+ websocket_handle /3 ,
44
+ websocket_info /3 ,
45
+ websocket_handle_event /3 ]).
46
+
38
47
39
48
websocket_init (Req , Opts ) ->
40
49
State = undefined ,
41
50
{ok , [], State }.
42
51
52
+
43
53
websocket_handle (_Req , {text , Data }, State ) ->
44
54
{reply , {text , Data }, State };
45
55
websocket_handle (_Req , {binary , Data }, State ) ->
46
56
{reply , {binary , Data }, State };
47
57
websocket_handle (_Req , _Frame , State ) ->
48
58
{ok , State }.
49
59
60
+
50
61
websocket_info (Req , Message , State ) ->
51
62
{ok , State }.
52
63
64
+
53
65
websocket_handle_event (Name , EventArgs , State ) ->
54
66
ok .
55
67
```
56
68
57
69
58
- ## Upgrading to WebSocket Connection
59
-
60
- This is an example of how you can upgrade to a WebSocket connection.
70
+ ### Upgrading to a WebSocket Connection
61
71
62
72
``` erlang
63
73
-module (elli_echo_websocket ).
@@ -68,6 +78,7 @@ This is an example of how you can upgrade to a WebSocket connection.
68
78
69
79
-behaviour (elli_handler ).
70
80
81
+
71
82
init (Req , Args ) ->
72
83
Method = case elli_request :get_header (<<" Upgrade" >>, Req ) of
73
84
<<" websocket" >> ->
@@ -76,6 +87,7 @@ init(Req, Args) ->
76
87
ignore
77
88
end .
78
89
90
+
79
91
handle (Req , Args ) ->
80
92
Method = case elli_request :get_header (<<" Upgrade" >>, Req ) of
81
93
<<" websocket" >> ->
@@ -85,9 +97,11 @@ handle(Req, Args) ->
85
97
end ,
86
98
handle (Method , elli_request :path (Req ), Req , Args ).
87
99
100
+
88
101
handle_event (_Event , _Data , _Args ) ->
89
102
ok .
90
103
104
+
91
105
% %
92
106
% % Helpers
93
107
% %
@@ -97,17 +111,16 @@ init_ws([<<"echo_websocket">>], _Req, _Args) ->
97
111
init_ws (_ , _ , _ ) ->
98
112
ignore .
99
113
114
+
100
115
handle ('websocket' , [<<" echo_websocket" >>], Req , Args ) ->
101
116
% % Upgrade to a websocket connection.
102
117
elli_websocket :upgrade (Req , Args ),
103
118
104
119
% % websocket is closed.
105
120
{close , <<>>};
106
-
107
121
handle ('GET' , [<<" echo_websocket" >>], _Req , _Args ) ->
108
122
% % We got a normal request, request was not upgraded.
109
123
{200 , [], <<" Use an upgrade request" >>};
110
-
111
124
handle (_ ,_ ,_ ,_ ) ->
112
125
ignore .
113
126
```
0 commit comments