blob: e2c9934f8be7e7b0ab923ed6be710c4d15c2713c [file] [log] [blame]
Andy Green775c0dd2010-10-29 14:15:22 +01001<!DOCTYPE html>
2<html lang="en">
3<head>
4 <meta charset=utf-8 />
Andy Greenab7d9332010-11-11 13:19:19 +00005 <title>Minimal Websocket test app</title>
Andy Green775c0dd2010-10-29 14:15:22 +01006</head>
7
8<body>
Andy Greenab7d9332010-11-11 13:19:19 +00009<h2>libwebsockets "dumb-increment-protocol" test applet</h2>
10The incrementing number is coming from the server.
11Click the button to send the server a websocket message to
12reset the number.<br><br>
13
14<table>
15 <tr>
16 <td align=center><input type=button id=offset value="Reset counter" onclick="reset();" ></td>
17 <td width=100 align=center><div id=number> </div></td>
Andy Greenfe2a0d22010-11-12 13:10:40 +000018 <td id=wsdi_statustd align=center><div id=wsdi_status>Not initialized</div></td>
19 </tr>
20</table>
21
22<h2>libwebsockets "lws-mirror-protocol" test applet</h2>
23Use the mouse to draw on the canvas below -- all other browser windows open
24on this page see your drawing in realtime and you can see any of theirs as
25well.
26<p>
27The lws-mirror protocol doesn't interpret what is being sent to it, it just
28re-sends it to every other websocket it has a connection with using that
29protocol, including the guy who sent the packet.
30<br><br>
31
32<table>
33 <tr>
34 <td>Drawing color:
35 <select id="color" onchange="update_color();">
36 <option value=#000000>Black</option>
37 <option value=#0000ff>Blue</option>
38 <option value=#20ff20>Green</option>
39 <option value=#802020>Dark Red</option>
40 </select>
41 </td>
42 <td id=wslm_statustd align=center><div id=wslm_status>Not initialized</div></td>
43 </tr>
44 <tr>
45 <td colspan=2 width=500 align=center style="background-color: #e0e0e0;"><div id=wslm_drawing> </div></td>
Andy Greenab7d9332010-11-11 13:19:19 +000046 </tr>
47</table>
48
Andy Green775c0dd2010-10-29 14:15:22 +010049<script>
50 var pos = 0;
51
Andy Greenfe2a0d22010-11-12 13:10:40 +000052function get_appropriate_ws_url(ads)
53{
Andy Green3faa9c72010-11-08 17:03:03 +000054 /*
55 * We open the websocket encrypted if this page came on an
56 * https:// url itself, otherwise unencrypted
57 */
58
59 if (document.URL.substring(0, 5) == "https")
Andy Greenfe2a0d22010-11-12 13:10:40 +000060 return "wss://"+ads;
Andy Green3faa9c72010-11-08 17:03:03 +000061 else
Andy Greenfe2a0d22010-11-12 13:10:40 +000062 return "ws://"+ads;
63}
64
65/* dumb increment protocol */
Andy Green3faa9c72010-11-08 17:03:03 +000066
Andy Greenfe2a0d22010-11-12 13:10:40 +000067 var socket_di = new WebSocket(get_appropriate_ws_url("127.0.0.1:7681"),
68 "dumb-increment-protocol");
Andy Green775c0dd2010-10-29 14:15:22 +010069
70 try {
Andy Greenfe2a0d22010-11-12 13:10:40 +000071 socket_di.onopen = function() {
72 wsdi_statustd.style.backgroundColor = "#40ff40";
73 wsdi_status.textContent = " websocket connection opened ";
Andy Greenab7d9332010-11-11 13:19:19 +000074 }
Andy Green775c0dd2010-10-29 14:15:22 +010075
Andy Greenfe2a0d22010-11-12 13:10:40 +000076 socket_di.onmessage =function got_packet(msg) {
Andy Greenab7d9332010-11-11 13:19:19 +000077 number.textContent = msg.data + "\n";
78 }
79
Andy Greenfe2a0d22010-11-12 13:10:40 +000080 socket_di.onclose = function(){
81 wsdi_statustd.style.backgroundColor = "#ff4040";
82 wsdi_status.textContent = " websocket connection closed ";
Andy Greenab7d9332010-11-11 13:19:19 +000083 }
84 } catch(exception) {
Andy Greenfe2a0d22010-11-12 13:10:40 +000085 alert('<p>Error' + exception);
Andy Greenab7d9332010-11-11 13:19:19 +000086 }
87
88function reset() {
Andy Greenfe2a0d22010-11-12 13:10:40 +000089 socket_di.send("reset\n");
Andy Greenab7d9332010-11-11 13:19:19 +000090}
91
Andy Greenfe2a0d22010-11-12 13:10:40 +000092
93/* lws-mirror protocol */
94
95 var down = 0;
96 var no_last = 1;
97 var last_x, last_y;
98 var ctx;
99 var socket_lm = new WebSocket(get_appropriate_ws_url("127.0.0.1:7681"),
100 "lws-mirror-protocol");
101 var color = "#000000";
102
103 try {
104 socket_lm.onopen = function() {
105 wslm_statustd.style.backgroundColor = "#40ff40";
106 wslm_status.textContent = " websocket connection opened ";
107 }
108
109 socket_lm.onmessage =function got_packet(msg) {
110 i = msg.data.split(' ');
111 if (i[0] == 'd') {
112 ctx.strokeStyle = i[1];
113 ctx.beginPath();
114 ctx.moveTo(i[2], i[3]);
115 ctx.lineTo(i[4], i[5]);
116 ctx.stroke();
117 }
118 }
119
120 socket_lm.onclose = function(){
121 wslm_statustd.style.backgroundColor = "#ff4040";
122 wslm_status.textContent = " websocket connection closed ";
123 }
124 } catch(exception) {
125 alert('<p>Error' + exception);
126 }
127
128 var canvas = document.createElement('canvas');
129 canvas.height = 300;
130 canvas.width = 480;
131 ctx = canvas.getContext("2d");
132
133 document.getElementById('wslm_drawing').appendChild(canvas);
134
135 canvas.addEventListener('mousemove', ev_mousemove, false);
136 canvas.addEventListener('mousedown', ev_mousedown, false);
137 canvas.addEventListener('mouseup', ev_mouseup, false);
138
139function update_color() {
140 color = document.getElementById("color").value;
141}
142
143function ev_mousedown (ev) {
144 down = 1;
145}
146
147function ev_mouseup(ev) {
148 down = 0;
149 no_last = 1;
150}
151
152function ev_mousemove (ev) {
153 var x, y;
154
155 x = ev.offsetX;
156 y = ev.offsetY;
157
158 if (!down)
159 return;
160 if (no_last) {
161 no_last = 0;
162 last_x = x;
163 last_y = y;
164 return;
165 }
166 socket_lm.send("d " + color + " " + last_x + " " + last_y + " " + x + ' ' + y);
167
168 last_x = x;
169 last_y = y;
170}
171
172
Andy Green775c0dd2010-10-29 14:15:22 +0100173</script>
174
175</body>
176</html>