-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathsimple_bridge_handler_sample.erl
90 lines (73 loc) · 2.75 KB
/
simple_bridge_handler_sample.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
83
84
85
86
87
88
89
%% vim: ts=4 sw=4 et
-module(simple_bridge_handler_sample).
-behaviour(simple_bridge_handler).
-export([run/1,
ws_init/1,
ws_message/3,
ws_info/3,
ws_terminate/3]).
run(Bridge) ->
Bridge2 = Bridge:set_response_data(body(Bridge)),
Bridge2:build_response().
ws_init(_Bridge) ->
%erlang:send_after(1000, self(), "START"),
ok.
ws_message({text, <<"frag">>}, _State, _Bridge) ->
Reply = [{text, [Msg," "]} || Msg <- ["A","spoon","full","of","sugar"]],
{reply, Reply};
ws_message({text, Data}, _State, _Bridge) ->
%Reply = io_lib:format("~s", [Data]),
{reply, {text, Data}};
ws_message({binary, Data}, _State, _Bridge) ->
{reply, {binary, Data}}.
ws_info(Data, _Bridge, _State) ->
Reply = {text, io_lib:format("~s", [Data])},
{reply, Reply}.
ws_terminate(_Reason, _Bridge, _State) ->
ok.
%%% HTML BODY AND DRAWING TESTING THE BRIDGE CAPABILITIES
welcome() ->
"Welcome to the Simple Bridge Websocket test.<br />Send <b>frag</b> to have the server send frames in fragments to the client.<br />Send <b>frag2</b> to send a fragmented message to the server from the client.".
body(Bridge) ->
[
<<"<html>
<head>
<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">
<title>Hello from Simple Bridge!</title>
<script src='http://code.jquery.com/jquery-1.10.2.js'></script>
<script src='/js/websocket.js'></script>
</head>
<body>
<h1>Hello from Simple Bridge!</h1>">>,
connection_info(Bridge),
<<"<div id='header'>
<div id='status'></div>
</div>
<div id='navigation'>
<p id='connecting'>
<input type='text' id='server' value='ws://localhost:8000/'></input>
<button type='button' onclick='toggle_connection()'>(re)connect websocket</button>
</p>
<div id='connected'>
<p>
<input type='text' id='send_txt' value=''></input>
<button type='button' onclick='sendTxt();'>send</button>
</p>
</div>
<div id='content'>
<button id='clear' onclick='clearScreen()' >Clear text</button>
<div id='output'></div>
</div>
</div>
</body>
</html>">>
].
connection_info(Bridge) ->
Fields = [socket, protocol, request_method, uri, path, headers, cookies, query_params, post_params, peer_ip, protocol_version],
[
<<"<table>">>,
[draw_connection_info(Field, Bridge) || Field <- Fields],
<<"</table>">>
].
draw_connection_info(Field, Bridge) ->
io_lib:format("<tr><td>~p</td><td><pre>~p</pre></td></tr>",[Field, sbw:Field(Bridge)]).