5 <title>Minimal Websocket test app</title>
9 <h3>Detected Browser: <div id=brow>...</div></h3>
10 <h2>libwebsockets "dumb-increment-protocol" test applet</h2>
11 The incrementing number is coming from the server and is individual for
12 each connection to the server... try opening a second browser window.
13 Click the button to send the server a websocket message to
14 reset the number.<br><br>
18 <td align=center><input type=button id=offset value="Reset counter" onclick="reset();" ></td>
19 <td width=100 align=center><div id=number> </div></td>
20 <td id=wsdi_statustd align=center><div id=wsdi_status>Not initialized</div></td>
24 <h2>libwebsockets "lws-mirror-protocol" test applet</h2>
25 Use the mouse to draw on the canvas below -- all other browser windows open
26 on this page see your drawing in realtime and you can see any of theirs as
29 The lws-mirror protocol doesn't interpret what is being sent to it, it just
30 re-sends it to every other websocket it has a connection with using that
31 protocol, including the guy who sent the packet.
32 <p>libwebsockets-test-client spams circles on to this shared canvas when
39 <select id="color" onchange="update_color();">
40 <option value=#000000>Black</option>
41 <option value=#0000ff>Blue</option>
42 <option value=#20ff20>Green</option>
43 <option value=#802020>Dark Red</option>
46 <td id=wslm_statustd align=center><div id=wslm_status>Not initialized</div></td>
49 <td colspan=2 width=500 align=center style="background-color: #e0e0e0;"><div id=wslm_drawing> </div></td>
55 /* BrowserDetect came from http://www.quirksmode.org/js/detect.html */
59 this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
60 this.version = this.searchVersion(navigator.userAgent)
61 || this.searchVersion(navigator.appVersion)
62 || "an unknown version";
63 this.OS = this.searchString(this.dataOS) || "an unknown OS";
65 searchString: function (data) {
66 for (var i=0;i<data.length;i++) {
67 var dataString = data[i].string;
68 var dataProp = data[i].prop;
69 this.versionSearchString = data[i].versionSearch || data[i].identity;
71 if (dataString.indexOf(data[i].subString) != -1)
72 return data[i].identity;
75 return data[i].identity;
78 searchVersion: function (dataString) {
79 var index = dataString.indexOf(this.versionSearchString);
80 if (index == -1) return;
81 return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
85 string: navigator.userAgent,
89 { string: navigator.userAgent,
91 versionSearch: "OmniWeb/",
95 string: navigator.vendor,
98 versionSearch: "Version"
103 versionSearch: "Version"
106 string: navigator.vendor,
111 string: navigator.vendor,
113 identity: "Konqueror"
116 string: navigator.userAgent,
117 subString: "Firefox",
121 string: navigator.vendor,
125 { // for newer Netscapes (6+)
126 string: navigator.userAgent,
127 subString: "Netscape",
131 string: navigator.userAgent,
133 identity: "Explorer",
134 versionSearch: "MSIE"
137 string: navigator.userAgent,
142 { // for older Netscapes (4-)
143 string: navigator.userAgent,
144 subString: "Mozilla",
145 identity: "Netscape",
146 versionSearch: "Mozilla"
151 string: navigator.platform,
156 string: navigator.platform,
161 string: navigator.userAgent,
163 identity: "iPhone/iPod"
166 string: navigator.platform,
173 BrowserDetect.init();
175 document.getElementById("brow").textContent = " " + BrowserDetect.browser + " "
176 + BrowserDetect.version +" " + BrowserDetect.OS +" ";
180 function get_appropriate_ws_url()
183 var u = document.URL;
186 * We open the websocket encrypted if this page came on an
187 * https:// url itself, otherwise unencrypted
190 if (u.substring(0, 5) == "https") {
195 if (u.substring(0, 4) == "http")
205 document.getElementById("number").textContent = get_appropriate_ws_url();
207 /* dumb increment protocol */
211 if (typeof MozWebSocket != "undefined") {
212 socket_di = new MozWebSocket(get_appropriate_ws_url(),
213 "dumb-increment-protocol");
215 socket_di = new WebSocket(get_appropriate_ws_url(),
216 "dumb-increment-protocol");
221 socket_di.onopen = function() {
222 document.getElementById("wsdi_statustd").style.backgroundColor = "#40ff40";
223 document.getElementById("wsdi_status").textContent = " websocket connection opened ";
226 socket_di.onmessage =function got_packet(msg) {
227 document.getElementById("number").textContent = msg.data + "\n";
230 socket_di.onclose = function(){
231 document.getElementById("wsdi_statustd").style.backgroundColor = "#ff4040";
232 document.getElementById("wsdi_status").textContent = " websocket connection CLOSED ";
235 alert('<p>Error' + exception);
239 socket_di.send("reset\n");
243 /* lws-mirror protocol */
247 var last_x = 0, last_y = 0;
250 var color = "#000000";
252 if (typeof MozWebSocket != "undefined") {
253 socket_lm = new MozWebSocket(get_appropriate_ws_url(),
254 "lws-mirror-protocol");
256 socket_lm = new WebSocket(get_appropriate_ws_url(),
257 "lws-mirror-protocol");
262 socket_lm.onopen = function() {
263 document.getElementById("wslm_statustd").style.backgroundColor = "#40ff40";
264 document.getElementById("wslm_status").textContent = " websocket connection opened ";
267 socket_lm.onmessage =function got_packet(msg) {
268 j = msg.data.split(';');
270 while (f < j.length - 1) {
273 ctx.strokeStyle = i[1];
275 ctx.moveTo(+(i[2]), +(i[3]));
276 ctx.lineTo(+(i[4]), +(i[5]));
280 ctx.strokeStyle = i[1];
282 ctx.arc(+(i[2]), +(i[3]), +(i[4]), 0, Math.PI*2, true);
290 socket_lm.onclose = function(){
291 document.getElementById("wslm_statustd").style.backgroundColor = "#ff4040";
292 document.getElementById("wslm_status").textContent = " websocket connection CLOSED ";
295 alert('<p>Error' + exception);
298 var canvas = document.createElement('canvas');
301 ctx = canvas.getContext("2d");
303 document.getElementById('wslm_drawing').appendChild(canvas);
305 canvas.addEventListener('mousemove', ev_mousemove, false);
306 canvas.addEventListener('mousedown', ev_mousedown, false);
307 canvas.addEventListener('mouseup', ev_mouseup, false);
309 offsetX = offsetY = 0;
311 if (element.offsetParent) {
313 offsetX += element.offsetLeft;
314 offsetY += element.offsetTop;
315 } while ((element = element.offsetParent));
318 function update_color() {
319 color = document.getElementById("color").value;
322 function ev_mousedown (ev) {
326 function ev_mouseup(ev) {
331 function ev_mousemove (ev) {
338 x = ev.layerX - offsetX;
339 y = ev.layerY - offsetY;
351 socket_lm.send("d " + color + " " + last_x + " " + last_y + " " + x + ' ' + y + ';');