4 <meta charset=utf-8 http-equiv="Content-Language" content="en"/>
5 <title>Minimal Websocket test app</title>
6 <style type="text/css">
7 div.title { font-size:18pt; font: Arial; font-weight:normal; text-align:center; color:#000000; }
8 .browser { font-size:18pt; font: Arial; font-weight:normal; text-align:center; color:#ffff00; vertical-align:middle; text-align:center; background:#d0b070; padding:12px; -webkit-border-radius:10px; -moz-border-radius:10px; border-radius:10px;}
9 .group2 { width:600px; vertical-align:middle; text-align:center; background:#f0f0e0; padding:12px; -webkit-border-radius:10px; -moz-border-radius:10px; border-radius:10px; }
10 .explain { vertical-align:middle; text-align:center; background:#f0f0c0; padding:12px; -webkit-border-radius:10px; -moz-border-radius:10px; border-radius:10px; color:#404000; }
11 .content { vertical-align:top; text-align:center; background:#fffff0; padding:12px; -webkit-border-radius:10px; -moz-border-radius:10px; border-radius:10px; }
12 .canvas { vertical-align:top; text-align:center; background:#efefd0; padding:12px; -webkit-border-radius:10px; -moz-border-radius:10px; border-radius:10px; }
22 <table width="100%"><tr><td valign=middle align=center><a href="http://libwebsockets.org"><img src="/libwebsockets.org-logo.png"></a></td><td>
23 <section class="browser">Detected Browser: <div id=brow>...</div></section></td></tr></table>
27 <section id="increment" class="group2">
28 <div class="title">libwebsockets "dumb-increment-protocol"</div>
30 <table class="content" width="200px">
31 <tr><td align=center><input type=button id=offset value="Reset counter" onclick="reset();" ></td></tr>
32 <tr><td width=200px align=center><div id=number> </div></td></tr>
33 <tr><td id=wsdi_statustd align=center class="explain"><div id=wsdi_status>Not initialized</div></td></tr>
36 </td><td class="explain">
37 The incrementing number is coming from the server and is individual for
38 each connection to the server... try opening a second browser window.
40 The button zeros just this connection's number.
42 Click <a href="/leaf.jpg" target="_blank">Here</a> to have the test server send a big picture by http.
46 <section id="mirror" class="group2">
47 <div class="title">libwebsockets "lws-mirror-protocol"</div>
49 Use the mouse to draw on the canvas below -- all other browser windows open
50 on this page see your drawing in realtime and you can see any of theirs as
53 The lws-mirror protocol doesn't interpret what is being sent to it, it just
54 re-sends it to every other websocket it has a connection with using that
55 protocol, including the guy who sent the packet.
57 <b>libwebsockets-test-client</b> joins in by spamming circles on to this shared canvas when
60 <table class="content">
63 <select id="color" onchange="update_color();">
64 <option value=#000000>Black</option>
65 <option value=#0000ff>Blue</option>
66 <option value=#20ff20>Green</option>
67 <option value=#802020>Dark Red</option>
70 <td id=wslm_statustd align=center class="explain"><div id=wslm_status>Not initialized</div></td>
73 <td colspan=2 width=500 class="content">
74 <div id="wslm_drawing">
80 <section id="ot" class="group2">
81 <div class="title">libwebsockets "open and close testing"</div>
83 <table class="content" width="200px">
85 <td align=center><input type=button id=ot_open_btn value="Open" onclick="ot_open();" ></td>
86 <td align=center><input type=button id=ot_close_btn disabled value="Close" onclick="ot_close();" ></td>
87 <td align=center><input type=button id=ot_req_close_btn disabled value="Request Server Close" onclick="ot_req_close();" ></td>
89 <tr><td colspan="3" id=ot_statustd align=center class="explain"><div id=ot_status>Not initialized</div></td></tr>
92 </td><td class="explain">
93 To help with open and close testing, you can open and close a connection by hand using
94 the buttons. "Request Server Close" sends a message asking the server to
101 Looking for support? <a href="http://libwebsockets.org">http://libwebsockets.org</a><br/>
102 Join the mailing list: <a href="http://ml.libwebsockets.org/mailman/listinfo/libwebsockets">http://ml.libwebsockets.org/mailman/listinfo/libwebsockets</a>
110 /* BrowserDetect came from http://www.quirksmode.org/js/detect.html */
112 var BrowserDetect = {
114 this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
115 this.version = this.searchVersion(navigator.userAgent)
116 || this.searchVersion(navigator.appVersion)
117 || "an unknown version";
118 this.OS = this.searchString(this.dataOS) || "an unknown OS";
120 searchString: function (data) {
121 for (var i=0;i<data.length;i++) {
122 var dataString = data[i].string;
123 var dataProp = data[i].prop;
124 this.versionSearchString = data[i].versionSearch || data[i].identity;
126 if (dataString.indexOf(data[i].subString) != -1)
127 return data[i].identity;
130 return data[i].identity;
133 searchVersion: function (dataString) {
134 var index = dataString.indexOf(this.versionSearchString);
135 if (index == -1) return;
136 return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
140 string: navigator.userAgent,
144 { string: navigator.userAgent,
145 subString: "OmniWeb",
146 versionSearch: "OmniWeb/",
150 string: navigator.vendor,
153 versionSearch: "Version"
158 versionSearch: "Version"
161 string: navigator.vendor,
166 string: navigator.vendor,
168 identity: "Konqueror"
171 string: navigator.userAgent,
172 subString: "Firefox",
176 string: navigator.vendor,
180 { // for newer Netscapes (6+)
181 string: navigator.userAgent,
182 subString: "Netscape",
186 string: navigator.userAgent,
188 identity: "Explorer",
189 versionSearch: "MSIE"
192 string: navigator.userAgent,
197 { // for older Netscapes (4-)
198 string: navigator.userAgent,
199 subString: "Mozilla",
200 identity: "Netscape",
201 versionSearch: "Mozilla"
206 string: navigator.platform,
211 string: navigator.platform,
216 string: navigator.userAgent,
218 identity: "iPhone/iPod"
221 string: navigator.platform,
228 BrowserDetect.init();
230 document.getElementById("brow").textContent = " " + BrowserDetect.browser + " "
231 + BrowserDetect.version +" " + BrowserDetect.OS +" ";
235 function get_appropriate_ws_url()
238 var u = document.URL;
241 * We open the websocket encrypted if this page came on an
242 * https:// url itself, otherwise unencrypted
245 if (u.substring(0, 5) == "https") {
250 if (u.substring(0, 4) == "http")
256 /* + "/xxx" bit is for IE10 workaround */
258 return pcol + u[0] + "/xxx";
262 document.getElementById("number").textContent = get_appropriate_ws_url();
264 /* dumb increment protocol */
268 if (typeof MozWebSocket != "undefined") {
269 socket_di = new MozWebSocket(get_appropriate_ws_url(),
270 "dumb-increment-protocol");
272 socket_di = new WebSocket(get_appropriate_ws_url(),
273 "dumb-increment-protocol");
278 socket_di.onopen = function() {
279 document.getElementById("wsdi_statustd").style.backgroundColor = "#40ff40";
280 document.getElementById("wsdi_status").innerHTML = " <b>websocket connection opened</b><br>" + socket_di.extensions;
283 socket_di.onmessage =function got_packet(msg) {
284 document.getElementById("number").textContent = msg.data + "\n";
287 socket_di.onclose = function(){
288 document.getElementById("wsdi_statustd").style.backgroundColor = "#ff4040";
289 document.getElementById("wsdi_status").textContent = " websocket connection CLOSED ";
292 alert('<p>Error' + exception);
296 socket_di.send("reset\n");
302 if (typeof MozWebSocket != "undefined") {
303 socket_ot = new MozWebSocket(get_appropriate_ws_url(),
304 "dumb-increment-protocol");
306 socket_ot = new WebSocket(get_appropriate_ws_url(),
307 "dumb-increment-protocol");
310 socket_ot.onopen = function() {
311 document.getElementById("ot_statustd").style.backgroundColor = "#40ff40";
312 document.getElementById("ot_status").innerHTML = " <b>websocket connection opened</b><br>" + socket_di.extensions;
313 document.getElementById("ot_open_btn").disabled = true;
314 document.getElementById("ot_close_btn").disabled = false;
315 document.getElementById("ot_req_close_btn").disabled = false;
318 socket_ot.onclose = function(e){
319 document.getElementById("ot_statustd").style.backgroundColor = "#ff4040";
320 document.getElementById("ot_status").textContent = " websocket connection CLOSED, code: " + e.code +
321 ", reason: " + e.reason;
322 document.getElementById("ot_open_btn").disabled = false;
323 document.getElementById("ot_close_btn").disabled = true;
324 document.getElementById("ot_req_close_btn").disabled = true;
327 alert('<p>Error' + exception);
331 /* browser will close the ws in a controlled way */
332 function ot_close() {
333 socket_ot.close(3000, "Bye!");
336 /* we ask the server to close the ws in a controlled way */
337 function ot_req_close() {
338 socket_ot.send("closeme\n");
341 /* lws-mirror protocol */
345 var last_x = 0, last_y = 0;
348 var color = "#000000";
350 if (typeof MozWebSocket != "undefined") {
351 socket_lm = new MozWebSocket(get_appropriate_ws_url(),
352 "lws-mirror-protocol");
354 socket_lm = new WebSocket(get_appropriate_ws_url(),
355 "lws-mirror-protocol");
360 socket_lm.onopen = function() {
361 document.getElementById("wslm_statustd").style.backgroundColor = "#40ff40";
362 document.getElementById("wslm_status").innerHTML = " <b>websocket connection opened</b><br>" + socket_di.extensions;
365 socket_lm.onmessage =function got_packet(msg) {
366 j = msg.data.split(';');
368 while (f < j.length - 1) {
371 ctx.strokeStyle = i[1];
373 ctx.moveTo(+(i[2]), +(i[3]));
374 ctx.lineTo(+(i[4]), +(i[5]));
378 ctx.strokeStyle = i[1];
380 ctx.arc(+(i[2]), +(i[3]), +(i[4]), 0, Math.PI*2, true);
388 socket_lm.onclose = function(){
389 document.getElementById("wslm_statustd").style.backgroundColor = "#ff4040";
390 document.getElementById("wslm_status").textContent = " websocket connection CLOSED ";
393 alert('<p>Error' + exception);
396 var canvas = document.createElement('canvas');
399 ctx = canvas.getContext("2d");
401 document.getElementById('wslm_drawing').appendChild(canvas);
403 canvas.addEventListener('mousemove', ev_mousemove, false);
404 canvas.addEventListener('mousedown', ev_mousedown, false);
405 canvas.addEventListener('mouseup', ev_mouseup, false);
407 offsetX = offsetY = 0;
409 if (element.offsetParent) {
411 offsetX += element.offsetLeft;
412 offsetY += element.offsetTop;
413 } while ((element = element.offsetParent));
416 function update_color() {
417 color = document.getElementById("color").value;
420 function ev_mousedown (ev) {
424 function ev_mouseup(ev) {
429 function ev_mousemove (ev) {
436 x = ev.layerX - offsetX;
437 y = ev.layerY - offsetY;
449 socket_lm.send("d " + color + " " + last_x + " " + last_y + " " + x + ' ' + y + ';');