-
Notifications
You must be signed in to change notification settings - Fork 0
/
ws.erl
82 lines (75 loc) · 1.94 KB
/
ws.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
-module(ws).
-export([ws_init/1, ws_send/3, ws_receive/1, ws_receive/2]).
ws_init(Sock) ->
case gen_tcp:recv(Sock, 0) of
{ok, Packet} ->
try ws_handshake:handshake(Packet) of
R ->
gen_tcp:send(Sock, R),
ok
catch
error ->
throw(error)
end;
{error, closed} ->
throw(closed);
{error,_} ->
throw(error)
end.
ws_send(Sock, Type, Content) ->
gen_tcp:send(Sock, ws_framing:buildMsg(Type, Content)).
ws_receive(Sock) -> ws_receive(Sock, normal).
ws_receive(Sock, normal) -> rcv(Sock, [], undefined, normal);
ws_receive(Sock, all) -> rcv(Sock, all);
ws_receive(Sock, raw) -> rcv(Sock, raw).
rcv(Sock, Acc, Type, normal) ->
case gen_tcp:recv(Sock, 0) of
{ok, Packet} ->
case ws_framing:parseMsg(Packet) of
{fragment, {Type,Con}} ->
rcv(Sock, Acc++Con, Type, normal);
{complete, {ping, Con}} ->
gen_tcp:send(Sock, ws_framing:buildMsg(pong, Con)),
rcv(Sock, [], undefined, normal);
{complete, {pong, _}} ->
rcv(Sock, [], undefined, normal);
{complete, {cont, Con}} ->
{complete, {Type, Acc++Con}};
{complete, P} ->
P
end;
{error, closed} ->
error;
{error,_} ->
error
end.
rcv(Sock, all) ->
case gen_tcp:recv(Sock, 0) of
{ok, Packet} ->
case ws_framing:parseMsg(Packet) of
{complete, {ping, Con}} ->
gen_tcp:send(Sock,
ws_framing:buildMsg(pong, Con)),
rcv(Sock, [], undefined, normal);
{complete, {pong, _}} ->
rcv(Sock, [], undefined, normal);
P ->
P
end;
{error, closed} ->
error;
{error,_} ->
error
end;
rcv(Sock, raw) ->
case gen_tcp:recv(Sock, 0) of
{ok, Packet} ->
case ws_framing:parseMsg(Packet) of
P ->
P
end;
{error, closed} ->
error;
{error,_} ->
error
end.