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>
256 <input type="radio" id="tab-5" name="tab-group-1">
257 <label for="tab-5">POST</label>
259 <div class="content">
260 <div id="ot" class="group2">
264 <span class="title">POST Form testing</span>
267 <td class="explain" colspan=2>
268 This tests POST handling in lws.
271 <td align=center colspan=2><div id=postinfo>
272 <form action="formtest" method="post">
274 <input type="text" name="Text" value="Give me some text"><br>
275 <input type="submit" value="Send the form">
287 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/>
288 Join the mailing list: <a href="https://libwebsockets.org/mailman/listinfo/libwebsockets">https://libwebsockets.org/mailman/listinfo/libwebsockets</a>
295 * We display untrusted stuff in html context... reject anything
296 * that has HTML stuff in it
301 if (s.search("<") != -1)
302 return "invalid string";
307 /* BrowserDetect came from http://www.quirksmode.org/js/detect.html */
309 var BrowserDetect = {
311 this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
312 this.version = this.searchVersion(navigator.userAgent)
313 || this.searchVersion(navigator.appVersion)
314 || "an unknown version";
315 this.OS = this.searchString(this.dataOS) || "an unknown OS";
317 searchString: function (data) {
318 for (var i=0;i<data.length;i++) {
319 var dataString = data[i].string;
320 var dataProp = data[i].prop;
321 this.versionSearchString = data[i].versionSearch || data[i].identity;
323 if (dataString.indexOf(data[i].subString) != -1)
324 return data[i].identity;
327 return data[i].identity;
330 searchVersion: function (dataString) {
331 var index = dataString.indexOf(this.versionSearchString);
332 if (index == -1) return;
333 return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
337 string: navigator.userAgent,
341 { string: navigator.userAgent,
342 subString: "OmniWeb",
343 versionSearch: "OmniWeb/",
347 string: navigator.vendor,
350 versionSearch: "Version"
355 versionSearch: "Version"
358 string: navigator.vendor,
363 string: navigator.vendor,
365 identity: "Konqueror"
368 string: navigator.userAgent,
369 subString: "Firefox",
373 string: navigator.vendor,
377 { // for newer Netscapes (6+)
378 string: navigator.userAgent,
379 subString: "Netscape",
383 string: navigator.userAgent,
385 identity: "Explorer",
386 versionSearch: "MSIE"
389 string: navigator.userAgent,
394 { // for older Netscapes (4-)
395 string: navigator.userAgent,
396 subString: "Mozilla",
397 identity: "Netscape",
398 versionSearch: "Mozilla"
403 string: navigator.platform,
408 string: navigator.platform,
413 string: navigator.userAgent,
415 identity: "iPhone/iPod"
418 string: navigator.platform,
425 BrowserDetect.init();
427 document.getElementById("brow").textContent = " " + BrowserDetect.browser + " "
428 + BrowserDetect.version +" " + BrowserDetect.OS +" ";
432 function get_appropriate_ws_url()
435 var u = document.URL;
438 * We open the websocket encrypted if this page came on an
439 * https:// url itself, otherwise unencrypted
442 if (u.substring(0, 5) == "https") {
447 if (u.substring(0, 4) == "http")
453 /* + "/xxx" bit is for IE10 workaround */
455 return pcol + u[0] + "/xxx";
459 document.getElementById("number").textContent = get_appropriate_ws_url();
461 /* dumb increment protocol */
465 if (typeof MozWebSocket != "undefined") {
466 socket_di = new MozWebSocket(get_appropriate_ws_url(),
467 "dumb-increment-protocol");
469 socket_di = new WebSocket(get_appropriate_ws_url(),
470 "dumb-increment-protocol");
475 socket_di.onopen = function() {
476 document.getElementById("wsdi_statustd").style.backgroundColor = "#40ff40";
477 document.getElementById("wsdi_status").innerHTML =
478 " <b>websocket connection opened</b><br>" +
479 san(socket_di.extensions);
482 socket_di.onmessage =function got_packet(msg) {
483 document.getElementById("number").textContent = msg.data + "\n";
486 socket_di.onclose = function(){
487 document.getElementById("wsdi_statustd").style.backgroundColor = "#ff4040";
488 document.getElementById("wsdi_status").textContent = " websocket connection CLOSED ";
491 alert('<p>Error' + exception);
494 var socket_status, jso, s;
496 if (typeof MozWebSocket != "undefined") {
497 socket_status = new MozWebSocket(get_appropriate_ws_url(),
500 socket_status = new WebSocket(get_appropriate_ws_url(),
506 socket_status.onopen = function() {
507 document.getElementById("s_statustd").style.backgroundColor = "#40ff40";
508 document.getElementById("s_status").innerHTML =
509 " <b>websocket connection opened</b><br>" +
510 san(socket_status.extensions);
513 socket_status.onmessage =function got_packet(msg) {
514 jso = JSON.parse(msg.data);
516 document.getElementById("servinfo").innerHTML =
517 "<table><tr><td class=l>Build info</td><td>"+
518 san(jso.version) + "</td></tr>" +
519 "<tr><td class=l>Server info</td><td>" +
520 san(jso.hostname) + "</td></tr>" +
524 for (n = 0; n < jso.conns.length; n++)
525 s = s + "<tr><td class=l>client " + (n + 1) +
526 "</td><td><b>" + san(jso.conns[n].peer) +
527 "</b><br>" + san(jso.conns[n].time) +
528 "<br>" + san(jso.conns[n].ua) +
532 document.getElementById("conninfo").innerHTML = s;
535 socket_status.onclose = function(){
536 document.getElementById("s_statustd").style.backgroundColor = "#ff4040";
537 document.getElementById("s_status").textContent = " websocket connection CLOSED ";
540 alert('<p>Error' + exception);
544 socket_di.send("reset\n");
549 for(var word = ''; word.length < 9000; word += 'a'){}
550 socket_di.send(word);
556 if (typeof MozWebSocket != "undefined") {
557 socket_ot = new MozWebSocket(get_appropriate_ws_url(),
558 "dumb-increment-protocol");
560 socket_ot = new WebSocket(get_appropriate_ws_url(),
561 "dumb-increment-protocol");
564 socket_ot.onopen = function() {
565 document.getElementById("ot_statustd").style.backgroundColor = "#40ff40";
566 document.getElementById("ot_status").innerHTML = " <b>websocket connection opened</b><br>" + san(socket_di.extensions);
567 document.getElementById("ot_open_btn").disabled = true;
568 document.getElementById("ot_close_btn").disabled = false;
569 document.getElementById("ot_req_close_btn").disabled = false;
572 socket_ot.onclose = function(e){
573 document.getElementById("ot_statustd").style.backgroundColor = "#ff4040";
574 document.getElementById("ot_status").textContent = " websocket connection CLOSED, code: " + e.code +
575 ", reason: " + e.reason;
576 document.getElementById("ot_open_btn").disabled = false;
577 document.getElementById("ot_close_btn").disabled = true;
578 document.getElementById("ot_req_close_btn").disabled = true;
581 alert('<p>Error' + exception);
585 /* browser will close the ws in a controlled way */
586 function ot_close() {
587 socket_ot.close(3000, "Bye!");
590 /* we ask the server to close the ws in a controlled way */
591 function ot_req_close() {
592 socket_ot.send("closeme\n");
595 /* lws-mirror protocol */
599 var last_x = 0, last_y = 0;
602 var color = "#000000";
604 if (typeof MozWebSocket != "undefined") {
605 socket_lm = new MozWebSocket(get_appropriate_ws_url(),
606 "lws-mirror-protocol");
608 socket_lm = new WebSocket(get_appropriate_ws_url(),
609 "lws-mirror-protocol");
614 socket_lm.onopen = function() {
615 document.getElementById("wslm_statustd").style.backgroundColor = "#40ff40";
616 document.getElementById("wslm_status").innerHTML =
617 " <b>websocket connection opened</b><br>" +
618 san(socket_lm.extensions);
621 socket_lm.onmessage =function got_packet(msg) {
622 j = msg.data.split(';');
624 while (f < j.length - 1) {
627 ctx.strokeStyle = i[1];
629 ctx.moveTo(+(i[2]), +(i[3]));
630 ctx.lineTo(+(i[4]), +(i[5]));
634 ctx.strokeStyle = i[1];
636 ctx.arc(+(i[2]), +(i[3]), +(i[4]), 0, Math.PI*2, true);
644 socket_lm.onclose = function(){
645 document.getElementById("wslm_statustd").style.backgroundColor = "#ff4040";
646 document.getElementById("wslm_status").textContent = " websocket connection CLOSED ";
649 alert('<p>Error' + exception);
652 var canvas = document.createElement('canvas');
655 ctx = canvas.getContext("2d");
657 document.getElementById('wslm_drawing').appendChild(canvas);
659 canvas.addEventListener('mousemove', ev_mousemove, false);
660 canvas.addEventListener('mousedown', ev_mousedown, false);
661 canvas.addEventListener('mouseup', ev_mouseup, false);
663 offsetX = offsetY = 0;
665 if (element.offsetParent) {
667 offsetX += element.offsetLeft;
668 offsetY += element.offsetTop;
669 } while ((element = element.offsetParent));
672 function update_color() {
673 color = document.getElementById("color").value;
676 function ev_mousedown (ev) {
680 function ev_mouseup(ev) {
685 function ev_mousemove (ev) {
692 x = ev.layerX - offsetX;
693 y = ev.layerY - offsetY;
705 socket_lm.send("d " + color + " " + last_x + " " + last_y + " " + x + ' ' + y + ';');