4 <meta charset=utf-8 http-equiv="Content-Language" content="en"/>
5 <title>Minimal Websocket test app</title>
6 <style type="text/css">
7 span.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 { vertical-align:middle;
13 -webkit-border-radius:10px;
14 -moz-border-radius:10px;
16 .explain { vertical-align:middle;
18 background:#f0f0c0; padding:12px;
19 -webkit-border-radius:10px;
20 -moz-border-radius:10px;
23 td.wsstatus { vertical-align:middle; width:200px; height:50px;
25 background:#f0f0c0; padding:6px;
26 -webkit-border-radius:8px;
27 -moz-border-radius:8px;
30 td.l { vertical-align:middle;
34 -webkit-border-radius:3px;
35 -moz-border-radius:3px;
37 .content { vertical-align:top; text-align:center; background:#fffff0; padding:12px; -webkit-border-radius:10px; -moz-border-radius:10px; border-radius:10px; }
38 .canvas { vertical-align:top; text-align:center; background:#efefd0; padding:12px; -webkit-border-radius:10px; -moz-border-radius:10px; border-radius:10px; }
41 min-height: 750px; /* This part sucks */
51 border: 1px solid #ccc;
67 border: 1px solid #ccc;
69 [type=radio]:checked ~ label {
71 border-bottom: 1px solid white;
74 [type=radio]:checked ~ label ~ .content {
88 <td valign=middle align=center>
89 <a href="https://libwebsockets.org">
90 <img src="libwebsockets.org-logo.png"></a></td><td>
91 <section class="browser">Detected Browser:
92 <div id=brow>...</div></section>
98 <tr><td colspan=2 align=center>
99 Click <a href="leaf.jpg" target="_blank">Here</a> to
100 have the test server send a big picture by http.
106 <input type="radio" id="tab-1" name="tab-group-1" checked>
107 <label for="tab-1">Dumb Increment Demo</label>
109 <div class="content">
110 <div id="dumb" class="group2">
113 <td id=wsdi_statustd align=center class="wsstatus">
114 <span id=wsdi_status>Websocket connection not initialized</span></td>
115 <td><span class="title">dumb increment-protocol</span></td>
118 <td class="explain" colspan=2>
119 The incrementing number is coming from the server at 20Hz and is individual for
120 each connection to the server... try opening a second browser window.
122 The button sends a message over the websocket link to ask the server
123 to zero just this connection's number.
127 <td align=center><div id=number style="font-size:120%;"> </div></td>
129 <input type=button id=offset value="Reset counter" onclick="reset();" >
130 <input type=button id=junk value="Send junk" onclick="junk();" >
139 <input type="radio" id="tab-2" name="tab-group-1">
140 <label for="tab-2">Mirror Demo</label>
142 <div class="content">
143 <div id="mirror" class="group2">
146 <td colspan=1 id=wslm_statustd align=center class="wsstatus">
147 <span id=wslm_status>Websocket connection not initialized</span>
150 <span class="title">lws-mirror-protocol</span>
155 <div class="explain">
156 Use the mouse to draw on the canvas below -- all other browser windows open
157 on this page see your drawing in realtime and you can see any of theirs as
160 The lws-mirror protocol doesn't interpret what is being sent to it, it just
161 re-sends it to every other websocket it has a connection with using that
162 protocol, including the guy who sent the packet.
164 <b>libwebsockets-test-client</b> joins in by spamming circles on to this shared canvas when
170 <td colspan=2>Drawing color:
171 <select id="color" onchange="update_color();">
172 <option value=#000000>Black</option>
173 <option value=#0000ff>Blue</option>
174 <option value=#20ff20>Green</option>
175 <option value=#802020>Dark Red</option>
179 <td colspan=2 width=500 height=320>
180 <div id="wslm_drawing" style="background:white"></div>
189 <input type="radio" id="tab-3" name="tab-group-1">
190 <label for="tab-3">Close Testing</label>
192 <div class="content">
193 <div id="ot" class="group2">
199 <tr><td id=ot_statustd align=center class="wsstatus">
200 <span id=ot_status>Websocket connection not initialized</span>
202 <td colspan=2><span class="title">Open and close testing</span></td>
205 <td class="explain" colspan=3 style="padding:3">
206 To help with open and close testing, you can open and close a connection by hand using
208 "<b>Close</b>" closes the connection from the browser with code 3000
209 and reason 'Bye!".<br>
210 "<b>Request Server Close</b>" sends a message asking the server to
211 initiate the close, which it does with code 1001 and reason "Seeya".
214 <td align=center><input type=button id=ot_open_btn value="Open" onclick="ot_open();" ></td>
215 <td align=center><input type=button id=ot_close_btn disabled value="Close" onclick="ot_close();" ></td>
216 <td align=center><input type=button id=ot_req_close_btn disabled value="Request Server Close" onclick="ot_req_close();" ></td>
226 <input type="radio" id="tab-4" name="tab-group-1">
227 <label for="tab-4">Server info</label>
229 <div class="content">
230 <div id="ot" class="group2">
233 <td id=s_statustd align=center class="wsstatus">
234 <div id=s_status>Websocket connection not initialized</div>
237 <span class="title">Server Info</span>
240 <td class="explain" colspan=2>
241 This information is sent by the server over a ws[s] link and updated live
242 whenever the information changes server-side.
245 <td align=center colspan=2><div id=servinfo></</div></td>
248 <td align=center colspan=2><div id=conninfo style="border : solid 2px #e0d040; padding : 4px; width : 500px; height : 350px; overflow : auto; "></</div></td>
258 Looking for support? <a href="https://libwebsockets.org">https://libwebsockets.org</a>, <a href="https://github.com/warmcat/libwebsockets">https://github.com/warmcat/libwebsockets</a></a><br/>
259 Join the mailing list: <a href="https://libwebsockets.org/mailman/listinfo/libwebsockets">https://libwebsockets.org/mailman/listinfo/libwebsockets</a>
266 * We display untrusted stuff in html context... reject anything
267 * that has HTML stuff in it
272 if (s.search("<") != -1)
273 return "invalid string";
278 /* BrowserDetect came from http://www.quirksmode.org/js/detect.html */
280 var BrowserDetect = {
282 this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
283 this.version = this.searchVersion(navigator.userAgent)
284 || this.searchVersion(navigator.appVersion)
285 || "an unknown version";
286 this.OS = this.searchString(this.dataOS) || "an unknown OS";
288 searchString: function (data) {
289 for (var i=0;i<data.length;i++) {
290 var dataString = data[i].string;
291 var dataProp = data[i].prop;
292 this.versionSearchString = data[i].versionSearch || data[i].identity;
294 if (dataString.indexOf(data[i].subString) != -1)
295 return data[i].identity;
298 return data[i].identity;
301 searchVersion: function (dataString) {
302 var index = dataString.indexOf(this.versionSearchString);
303 if (index == -1) return;
304 return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
308 string: navigator.userAgent,
312 { string: navigator.userAgent,
313 subString: "OmniWeb",
314 versionSearch: "OmniWeb/",
318 string: navigator.vendor,
321 versionSearch: "Version"
326 versionSearch: "Version"
329 string: navigator.vendor,
334 string: navigator.vendor,
336 identity: "Konqueror"
339 string: navigator.userAgent,
340 subString: "Firefox",
344 string: navigator.vendor,
348 { // for newer Netscapes (6+)
349 string: navigator.userAgent,
350 subString: "Netscape",
354 string: navigator.userAgent,
356 identity: "Explorer",
357 versionSearch: "MSIE"
360 string: navigator.userAgent,
365 { // for older Netscapes (4-)
366 string: navigator.userAgent,
367 subString: "Mozilla",
368 identity: "Netscape",
369 versionSearch: "Mozilla"
374 string: navigator.platform,
379 string: navigator.platform,
384 string: navigator.userAgent,
386 identity: "iPhone/iPod"
389 string: navigator.platform,
396 BrowserDetect.init();
398 document.getElementById("brow").textContent = " " + BrowserDetect.browser + " "
399 + BrowserDetect.version +" " + BrowserDetect.OS +" ";
403 function get_appropriate_ws_url()
406 var u = document.URL;
409 * We open the websocket encrypted if this page came on an
410 * https:// url itself, otherwise unencrypted
413 if (u.substring(0, 5) == "https") {
418 if (u.substring(0, 4) == "http")
424 /* + "/xxx" bit is for IE10 workaround */
426 return pcol + u[0] + "/xxx";
430 document.getElementById("number").textContent = get_appropriate_ws_url();
432 /* dumb increment protocol */
436 if (typeof MozWebSocket != "undefined") {
437 socket_di = new MozWebSocket(get_appropriate_ws_url(),
438 "dumb-increment-protocol");
440 socket_di = new WebSocket(get_appropriate_ws_url(),
441 "dumb-increment-protocol");
446 socket_di.onopen = function() {
447 document.getElementById("wsdi_statustd").style.backgroundColor = "#40ff40";
448 document.getElementById("wsdi_status").innerHTML =
449 " <b>websocket connection opened</b><br>" +
450 san(socket_di.extensions);
453 socket_di.onmessage =function got_packet(msg) {
454 document.getElementById("number").textContent = msg.data + "\n";
457 socket_di.onclose = function(){
458 document.getElementById("wsdi_statustd").style.backgroundColor = "#ff4040";
459 document.getElementById("wsdi_status").textContent = " websocket connection CLOSED ";
462 alert('<p>Error' + exception);
465 var socket_status, jso, s;
467 if (typeof MozWebSocket != "undefined") {
468 socket_status = new MozWebSocket(get_appropriate_ws_url(),
471 socket_status = new WebSocket(get_appropriate_ws_url(),
477 socket_status.onopen = function() {
478 document.getElementById("s_statustd").style.backgroundColor = "#40ff40";
479 document.getElementById("s_status").innerHTML =
480 " <b>websocket connection opened</b><br>" +
481 san(socket_status.extensions);
484 socket_status.onmessage =function got_packet(msg) {
485 jso = JSON.parse(msg.data);
487 document.getElementById("servinfo").innerHTML =
488 "<table><tr><td class=l>Build info</td><td>"+
489 san(jso.version) + "</td></tr>" +
490 "<tr><td class=l>Server info</td><td>" +
491 san(jso.hostname) + "</td></tr>" +
495 for (n = 0; n < jso.conns.length; n++)
496 s = s + "<tr><td class=l>client " + (n + 1) +
497 "</td><td><b>" + san(jso.conns[n].peer) +
498 "</b><br>" + san(jso.conns[n].time) +
499 "<br>" + san(jso.conns[n].ua) +
503 document.getElementById("conninfo").innerHTML = s;
506 socket_status.onclose = function(){
507 document.getElementById("s_statustd").style.backgroundColor = "#ff4040";
508 document.getElementById("s_status").textContent = " websocket connection CLOSED ";
511 alert('<p>Error' + exception);
515 socket_di.send("reset\n");
520 for(var word = ''; word.length < 9000; word += 'a'){}
521 socket_di.send(word);
527 if (typeof MozWebSocket != "undefined") {
528 socket_ot = new MozWebSocket(get_appropriate_ws_url(),
529 "dumb-increment-protocol");
531 socket_ot = new WebSocket(get_appropriate_ws_url(),
532 "dumb-increment-protocol");
535 socket_ot.onopen = function() {
536 document.getElementById("ot_statustd").style.backgroundColor = "#40ff40";
537 document.getElementById("ot_status").innerHTML = " <b>websocket connection opened</b><br>" + san(socket_di.extensions);
538 document.getElementById("ot_open_btn").disabled = true;
539 document.getElementById("ot_close_btn").disabled = false;
540 document.getElementById("ot_req_close_btn").disabled = false;
543 socket_ot.onclose = function(e){
544 document.getElementById("ot_statustd").style.backgroundColor = "#ff4040";
545 document.getElementById("ot_status").textContent = " websocket connection CLOSED, code: " + e.code +
546 ", reason: " + e.reason;
547 document.getElementById("ot_open_btn").disabled = false;
548 document.getElementById("ot_close_btn").disabled = true;
549 document.getElementById("ot_req_close_btn").disabled = true;
552 alert('<p>Error' + exception);
556 /* browser will close the ws in a controlled way */
557 function ot_close() {
558 socket_ot.close(3000, "Bye!");
561 /* we ask the server to close the ws in a controlled way */
562 function ot_req_close() {
563 socket_ot.send("closeme\n");
566 /* lws-mirror protocol */
570 var last_x = 0, last_y = 0;
573 var color = "#000000";
575 if (typeof MozWebSocket != "undefined") {
576 socket_lm = new MozWebSocket(get_appropriate_ws_url(),
577 "lws-mirror-protocol");
579 socket_lm = new WebSocket(get_appropriate_ws_url(),
580 "lws-mirror-protocol");
585 socket_lm.onopen = function() {
586 document.getElementById("wslm_statustd").style.backgroundColor = "#40ff40";
587 document.getElementById("wslm_status").innerHTML =
588 " <b>websocket connection opened</b><br>" +
589 san(socket_lm.extensions);
592 socket_lm.onmessage =function got_packet(msg) {
593 j = msg.data.split(';');
595 while (f < j.length - 1) {
598 ctx.strokeStyle = i[1];
600 ctx.moveTo(+(i[2]), +(i[3]));
601 ctx.lineTo(+(i[4]), +(i[5]));
605 ctx.strokeStyle = i[1];
607 ctx.arc(+(i[2]), +(i[3]), +(i[4]), 0, Math.PI*2, true);
615 socket_lm.onclose = function(){
616 document.getElementById("wslm_statustd").style.backgroundColor = "#ff4040";
617 document.getElementById("wslm_status").textContent = " websocket connection CLOSED ";
620 alert('<p>Error' + exception);
623 var canvas = document.createElement('canvas');
626 ctx = canvas.getContext("2d");
628 document.getElementById('wslm_drawing').appendChild(canvas);
630 canvas.addEventListener('mousemove', ev_mousemove, false);
631 canvas.addEventListener('mousedown', ev_mousedown, false);
632 canvas.addEventListener('mouseup', ev_mouseup, false);
634 offsetX = offsetY = 0;
636 if (element.offsetParent) {
638 offsetX += element.offsetLeft;
639 offsetY += element.offsetTop;
640 } while ((element = element.offsetParent));
643 function update_color() {
644 color = document.getElementById("color").value;
647 function ev_mousedown (ev) {
651 function ev_mouseup(ev) {
656 function ev_mousemove (ev) {
663 x = ev.layerX - offsetX;
664 y = ev.layerY - offsetY;
676 socket_lm.send("d " + color + " " + last_x + " " + last_y + " " + x + ' ' + y + ';');