forked from erlyaws/yaws
/
websockets_autobahn_endpoint.yaws
43 lines (36 loc) · 1.34 KB
/
websockets_autobahn_endpoint.yaws
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
<erl>
out(A) ->
io:format("Spawning websocket owner~n",[]),
WebSocketOwner = spawn(fun() -> websocket_owner() end),
{websocket, WebSocketOwner, {active,true}}.
websocket_owner() ->
receive
{ok, WebSocket} ->
echo_server(WebSocket);
Any ->
io:format("websocket_owner received msg:~p~n", [Any])
end.
echo_server(WebSocket = {Socket, ProtocolVersion}) ->
receive
{tcp, Socket, DataFrame} ->
Data = yaws_api:websocket_unframe_data(ProtocolVersion, DataFrame),
CharCount = length(binary_to_list(Data)),
io:format("Text Chars Count = ~p~n", [CharCount]),
if
CharCount < 1000 ->
io:format("Got data from Websocket: ~p~n", [Data]);
true ->
io:format("Too many chars to print easily...~n",[])
end,
yaws_api:websocket_send(WebSocket, Data),
io:format("Frame: ~p~n", [frame_info(DataFrame)]),
echo_server(WebSocket);
{tcp_closed, Socket} ->
io:format("Websocket closed. Terminating echo_server...~n");
Any ->
io:format("echo_server received msg:~p~n", [Any]),
echo_server(WebSocket)
end.
frame_info(Frame = <<Fin:1,Rsv1:1,Rsv2:1,Rsv3:1,Opcode:4,Masked:1,Len1:7,Rest1/binary>>) ->
FrameInfo = [{fin, Fin},{rsv1,Rsv1},{rsv2,Rsv2},{rsv3,Rsv3},{opcode,Opcode},{masked,Masked},{len1,Len1}].
</erl>