4 <meta charset=utf-8 http-equiv="Content-Language" content="en"/>
5 <script src="/lws-common.js"></script>
6 <title>Minimal Websocket test app</title>
7 <style type="text/css">
8 span.title { font-size:18pt; font: Arial; font-weight:normal; text-align:center; color:#000000; }
9 .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;}
10 .group2 { vertical-align:middle;
14 -webkit-border-radius:10px;
15 -moz-border-radius:10px;
17 .explain { vertical-align:middle;
19 background:#f0f0c0; padding:12px;
20 -webkit-border-radius:10px;
21 -moz-border-radius:10px;
24 td.wsstatus { vertical-align:middle; width:200px; height:50px;
26 background:#f0f0c0; padding:6px;
27 -webkit-border-radius:8px;
28 -moz-border-radius:8px;
31 .tdform { vertical-align:middle; width:200px; height:50px;
33 background:#f0f0d0; padding:6px;
34 -webkit-border-radius:8px;
35 -moz-border-radius:8px; margin:10px;
37 border: 1px solid black;
38 border-collapse: collapse;font-size:18pt; font: Arial; font-weight:normal; text-align:center; color:#000000;
41 td.l { vertical-align:middle;
45 -webkit-border-radius:3px;
46 -moz-border-radius:3px;
48 .content { vertical-align:top; text-align:center; background:#fffff0; padding:12px; -webkit-border-radius:10px; -moz-border-radius:10px; border-radius:10px; }
49 .canvas { vertical-align:top; text-align:center; background:#efefd0; padding:12px; -webkit-border-radius:10px; -moz-border-radius:10px; border-radius:10px; }
52 min-height: 750px; /* This part sucks */
62 border: 1px solid #ccc;
78 border: 1px solid #ccc;
80 [type=radio]:checked ~ label {
82 border-bottom: 1px solid white;
85 [type=radio]:checked ~ label ~ .content {
99 <td valign=middle align=center>
100 <a href="https://libwebsockets.org">
101 <img src="libwebsockets.org-logo.png"></a></td><td>
102 <section class="browser">Detected Browser:
103 <div id=brow>...</div></section>
109 <tr><td colspan=2 align=center>
110 Click <a href="leaf.jpg" target="_blank">Here</a> to
111 have the test server send a big picture by http.
117 <input type="radio" id="tab-1" name="tab-group-1" checked>
118 <label for="tab-1">Dumb Increment Demo</label>
120 <div class="content">
121 <div id="dumb" class="group2">
124 <td id=wsdi_statustd align=center class="wsstatus">
125 <span id=wsdi_status>Websocket connection not initialized</span></td>
126 <td><span class="title">dumb increment-protocol</span></td>
129 <td class="explain" colspan=2>
130 The incrementing number is coming from the server at 20Hz and is individual for
131 each connection to the server... try opening a second browser window.
133 The button sends a message over the websocket link to ask the server
134 to zero just this connection's number.
138 <td align=center><div id=number style="font-size:120%;"> </div></td>
140 <input type=button id=offset value="Reset counter">
141 <input type=button id=junk value="Send junk">
150 <input type="radio" id="tab-2" name="tab-group-1">
151 <label for="tab-2">Mirror Demo</label>
153 <div class="content">
154 <div id="mirror" class="group2">
157 <td colspan=1 id=wslm_statustd align=center class="wsstatus">
158 <span id=wslm_status>Websocket connection not initialized</span>
161 <span class="title">lws-mirror-protocol</span>
166 <div class="explain">
167 Use the mouse to draw on the canvas below -- all other browser windows open
168 on this page see your drawing in realtime and you can see any of theirs as
171 The lws-mirror protocol doesn't interpret what is being sent to it, it just
172 re-sends it to every other websocket it has a connection with using that
173 protocol, including the guy who sent the packet.
175 <b>libwebsockets-test-client</b> joins in by spamming circles on to this shared canvas when
181 <td colspan=2>Drawing color:
183 <option value=#000000>Black</option>
184 <option value=#0000ff>Blue</option>
185 <option value=#20ff20>Green</option>
186 <option value=#802020>Dark Red</option>
190 <td colspan=2 width=500 height=320>
191 <div id="wslm_drawing" style="background:white"></div>
200 <input type="radio" id="tab-3" name="tab-group-1">
201 <label for="tab-3">Close Testing</label>
203 <div class="content">
204 <div id="ot" class="group2">
210 <tr><td id=ot_statustd align=center class="wsstatus">
211 <span id=ot_status>Websocket connection not initialized</span>
213 <td colspan=2><span class="title">Open and close testing</span></td>
216 <td class="explain" colspan=3 style="padding:3">
217 To help with open and close testing, you can open and close a connection by hand using
219 "<b>Close</b>" closes the connection from the browser with code 3000
220 and reason 'Bye!".<br>
221 "<b>Request Server Close</b>" sends a message asking the server to
222 initiate the close, which it does with code 1001 and reason "Seeya".
225 <td align=center><input type=button id=ot_open_btn value="Open"></td>
226 <td align=center><input type=button id=ot_close_btn disabled value="Close" ></td>
227 <td align=center><input type=button id=ot_req_close_btn disabled value="Request Server Close" ></td>
237 <input type="radio" id="tab-4" name="tab-group-1">
238 <label for="tab-4">Server info</label>
240 <div class="content">
241 <div id="ot" class="group2">
244 <td id=s_statustd align=center class="wsstatus">
245 <div id=s_status>Websocket connection not initialized</div>
248 <span class="title">Server Info</span>
251 <td class="explain" colspan=2>
252 This information is sent by the server over a ws[s] link and updated live
253 whenever the information changes server-side.
256 <td align=center colspan=2><div id=servinfo></div></td>
259 <td align=center colspan=2><div id=conninfo style="border : solid 2px #e0d040; padding : 4px; width : 500px; height : 350px; overflow : auto; "></</div></td>
267 <input type="radio" id="tab-5" name="tab-group-1">
268 <label for="tab-5">POST</label>
270 <div class="content">
271 <div id="ot" class="group2">
275 <span class="title">POST Form testing</span>
278 <td class="explain" colspan=2>
279 This tests POST handling in lws.
282 <td align=center colspan=2 class=tdform><div id=postinfo style=form>
283 FORM 1: send with urlencoded POST body args<br>
284 <form action="formtest" method="post">
285 <span style="font-size:12pt;">Some text: </span>
286 <input type="text" name="text" value="Give me some text"><br>
287 <input type="submit" name="send" value="Send the form">
292 <script nonce="lwscaro">
293 function check_file()
295 var f = document.getElementById('file').files[0];
296 var max_len = 100000;
300 if (f.size >= max_len) {
302 document.getElementById('file_info').innerHTML =
303 "<span style=\"color:red;font-weight:bold\">File larger than "+max_len+"</span>";
305 document.getElementById('file_info').innerHTML =
306 "File length "+f.size;
310 document.getElementById('upload').disabled = dis;
315 <td align=center colspan=2 class=tdform><div id=postinfo style=form>
316 FORM 2: send with multipart/form-data<br>
317 (can handle file upload, test limited to 100KB)<br>
318 <form name=multipart action="formtest" method="post" enctype="multipart/form-data">
319 <span style="font-size:12pt;">Some text: </span>
320 <input type="text" name="text" value="Give me some text">
322 <input type="file" name="file" id="file" size="20"> <span id=file_info style="font-size:12pt;"></span><br>
323 <input type="submit" id="upload" name="upload" disabled=1 value="Upload">
336 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/>
337 Join the mailing list: <a href="https://libwebsockets.org/mailman/listinfo/libwebsockets">https://libwebsockets.org/mailman/listinfo/libwebsockets</a>
341 <script nonce="lwscaro">
343 document.getElementById('file').onchange = check_file;
344 document.getElementById('offset').onclick = reset;
345 document.getElementById('junk').onclick = junk;
346 document.getElementById('color').onclick = update_color;
347 document.getElementById('ot_open_btn').onclick = ot_open;
348 document.getElementById('ot_close_btn').onclick = ot_close;
349 document.getElementById('ot_req_close_btn').onclick = ot_req_close;
352 * We display untrusted stuff in html context... reject anything
353 * that has HTML stuff in it
358 if (s.search("<") != -1)
359 return "invalid string";
364 lws_gray_out(true,{'zindex':'499'});
366 /* BrowserDetect came from http://www.quirksmode.org/js/detect.html */
368 var BrowserDetect = {
370 this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
371 this.version = this.searchVersion(navigator.userAgent)
372 || this.searchVersion(navigator.appVersion)
373 || "an unknown version";
374 this.OS = this.searchString(this.dataOS) || "an unknown OS";
376 searchString: function (data) {
377 for (var i=0;i<data.length;i++) {
378 var dataString = data[i].string;
379 var dataProp = data[i].prop;
380 this.versionSearchString = data[i].versionSearch || data[i].identity;
382 if (dataString.indexOf(data[i].subString) != -1)
383 return data[i].identity;
386 return data[i].identity;
389 searchVersion: function (dataString) {
390 var index = dataString.indexOf(this.versionSearchString);
391 if (index == -1) return;
392 return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
396 string: navigator.userAgent,
400 { string: navigator.userAgent,
401 subString: "OmniWeb",
402 versionSearch: "OmniWeb/",
406 string: navigator.vendor,
409 versionSearch: "Version"
414 versionSearch: "Version"
417 string: navigator.vendor,
422 string: navigator.vendor,
424 identity: "Konqueror"
427 string: navigator.userAgent,
428 subString: "Firefox",
432 string: navigator.vendor,
436 { // for newer Netscapes (6+)
437 string: navigator.userAgent,
438 subString: "Netscape",
442 string: navigator.userAgent,
444 identity: "Explorer",
445 versionSearch: "MSIE"
448 string: navigator.userAgent,
453 { // for older Netscapes (4-)
454 string: navigator.userAgent,
455 subString: "Mozilla",
456 identity: "Netscape",
457 versionSearch: "Mozilla"
462 string: navigator.platform,
467 string: navigator.platform,
472 string: navigator.userAgent,
474 identity: "iPhone/iPod"
477 string: navigator.platform,
484 BrowserDetect.init();
486 document.getElementById("brow").textContent = " " + BrowserDetect.browser + " "
487 + BrowserDetect.version +" " + BrowserDetect.OS +" ";
491 function get_appropriate_ws_url()
494 var u = document.URL;
497 * We open the websocket encrypted if this page came on an
498 * https:// url itself, otherwise unencrypted
501 if (u.substring(0, 5) == "https") {
506 if (u.substring(0, 4) == "http")
512 /* + "/xxx" bit is for IE10 workaround */
514 return pcol + u[0] + "/xxx";
518 document.getElementById("number").textContent = get_appropriate_ws_url();
520 /* dumb increment protocol */
524 if (typeof MozWebSocket != "undefined") {
525 socket_di = new MozWebSocket(get_appropriate_ws_url(),
526 "dumb-increment-protocol");
528 socket_di = new WebSocket(get_appropriate_ws_url(),
529 "dumb-increment-protocol");
534 socket_di.onopen = function() {
535 document.getElementById("wsdi_statustd").style.backgroundColor = "#40ff40";
536 document.getElementById("wsdi_status").innerHTML =
537 " <b>websocket connection opened</b><br>" +
538 san(socket_di.extensions);
541 socket_di.onmessage =function got_packet(msg) {
542 document.getElementById("number").textContent = msg.data + "\n";
545 socket_di.onclose = function(){
546 document.getElementById("wsdi_statustd").style.backgroundColor = "#ff4040";
547 document.getElementById("wsdi_status").textContent = " websocket connection CLOSED ";
550 alert('<p>Error' + exception);
553 var socket_status, jso, s;
555 if (typeof MozWebSocket != "undefined") {
556 socket_status = new MozWebSocket(get_appropriate_ws_url(),
559 socket_status = new WebSocket(get_appropriate_ws_url(),
565 socket_status.onopen = function() {
566 document.getElementById("s_statustd").style.backgroundColor = "#40ff40";
567 document.getElementById("s_status").innerHTML =
568 " <b>websocket connection opened</b><br>" +
569 san(socket_status.extensions);
572 socket_status.onmessage =function got_packet(msg) {
575 jso = JSON.parse(msg.data);
577 document.getElementById("servinfo").innerHTML =
578 "<table><tr><td class=l>Build info</td><td>"+
579 san(jso.version) + "</td></tr>" +
580 "<tr><td class=l>Server info</td><td>" +
581 san(jso.hostname) + "</td></tr>" +
585 for (n = 0; n < jso.conns.length; n++) {
586 var d = new Date(parseInt(jso.conns[n].time) * 1000);
588 s = s + "<tr><td class=l>client " + (n + 1) +
589 "</td><td><b>" + san(jso.conns[n].peer) +
590 "</b><br>" + san(d.toString()) +
591 "<br>" + san(jso.conns[n].ua) +
596 document.getElementById("conninfo").innerHTML = s;
599 socket_status.onclose = function(){
600 document.getElementById("s_statustd").style.backgroundColor = "#ff4040";
601 document.getElementById("s_status").textContent = " websocket connection CLOSED ";
604 alert('<p>Error' + exception);
608 socket_di.send("reset\n");
613 for(var word = ''; word.length < 9000; word += 'a'){}
614 socket_di.send(word);
620 if (typeof MozWebSocket != "undefined") {
621 socket_ot = new MozWebSocket(get_appropriate_ws_url(),
622 "dumb-increment-protocol");
624 socket_ot = new WebSocket(get_appropriate_ws_url(),
625 "dumb-increment-protocol");
628 socket_ot.onopen = function() {
629 document.getElementById("ot_statustd").style.backgroundColor = "#40ff40";
630 document.getElementById("ot_status").innerHTML = " <b>websocket connection opened</b><br>" + san(socket_di.extensions);
631 document.getElementById("ot_open_btn").disabled = true;
632 document.getElementById("ot_close_btn").disabled = false;
633 document.getElementById("ot_req_close_btn").disabled = false;
636 socket_ot.onclose = function(e){
637 document.getElementById("ot_statustd").style.backgroundColor = "#ff4040";
638 document.getElementById("ot_status").textContent = " websocket connection CLOSED, code: " + e.code +
639 ", reason: " + e.reason;
640 document.getElementById("ot_open_btn").disabled = false;
641 document.getElementById("ot_close_btn").disabled = true;
642 document.getElementById("ot_req_close_btn").disabled = true;
645 alert('<p>Error' + exception);
649 /* browser will close the ws in a controlled way */
650 function ot_close() {
651 socket_ot.close(3000, "Bye!");
654 /* we ask the server to close the ws in a controlled way */
655 function ot_req_close() {
656 socket_ot.send("closeme\n");
659 /* lws-mirror protocol */
663 var last_x = 0, last_y = 0;
666 var color = "#000000";
668 if (typeof MozWebSocket != "undefined") {
669 socket_lm = new MozWebSocket(get_appropriate_ws_url(),
670 "lws-mirror-protocol");
672 socket_lm = new WebSocket(get_appropriate_ws_url(),
673 "lws-mirror-protocol");
678 socket_lm.onopen = function() {
679 document.getElementById("wslm_statustd").style.backgroundColor = "#40ff40";
680 document.getElementById("wslm_status").innerHTML =
681 " <b>websocket connection opened</b><br>" +
682 san(socket_lm.extensions);
686 socket_lm.onmessage =function got_packet(msg) {
687 j = msg.data.split(';');
689 while (f < j.length - 1) {
692 ctx.strokeStyle = i[1];
694 ctx.moveTo(+(i[2]), +(i[3]));
695 ctx.lineTo(+(i[4]), +(i[5]));
699 ctx.strokeStyle = i[1];
701 ctx.arc(+(i[2]), +(i[3]), +(i[4]), 0, Math.PI*2, true);
709 socket_lm.onclose = function(){
710 document.getElementById("wslm_statustd").style.backgroundColor = "#ff4040";
711 document.getElementById("wslm_status").textContent = " websocket connection CLOSED ";
712 lws_gray_out(true,{'zindex':'499'});
715 alert('<p>Error' + exception);
718 var canvas = document.createElement('canvas');
721 ctx = canvas.getContext("2d");
723 document.getElementById('wslm_drawing').appendChild(canvas);
725 canvas.addEventListener('mousemove', ev_mousemove, false);
726 canvas.addEventListener('mousedown', ev_mousedown, false);
727 canvas.addEventListener('mouseup', ev_mouseup, false);
729 offsetX = offsetY = 0;
731 if (element.offsetParent) {
733 offsetX += element.offsetLeft;
734 offsetY += element.offsetTop;
735 } while ((element = element.offsetParent));
738 function update_color() {
739 color = document.getElementById("color").value;
742 function ev_mousedown (ev) {
746 function ev_mouseup(ev) {
751 function ev_mousemove (ev) {
758 x = ev.layerX - offsetX;
759 y = ev.layerY - offsetY;
771 socket_lm.send("d " + color + " " + last_x + " " + last_y + " " + x + ' ' + y + ';');