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" onclick="reset();" >
141 <input type=button id=junk value="Send junk" onclick="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:
182 <select id="color" onchange="update_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" onclick="ot_open();" ></td>
226 <td align=center><input type=button id=ot_close_btn disabled value="Close" onclick="ot_close();" ></td>
227 <td align=center><input type=button id=ot_req_close_btn disabled value="Request Server Close" onclick="ot_req_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">
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"
323 onchange="check_file();"> <span id=file_info style="font-size:12pt;"></span><br>
324 <input type="submit" id="upload" name="upload" disabled=1 value="Upload">
337 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/>
338 Join the mailing list: <a href="https://libwebsockets.org/mailman/listinfo/libwebsockets">https://libwebsockets.org/mailman/listinfo/libwebsockets</a>
345 * We display untrusted stuff in html context... reject anything
346 * that has HTML stuff in it
351 if (s.search("<") != -1)
352 return "invalid string";
357 lws_gray_out(true,{'zindex':'499'});
359 /* BrowserDetect came from http://www.quirksmode.org/js/detect.html */
361 var BrowserDetect = {
363 this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
364 this.version = this.searchVersion(navigator.userAgent)
365 || this.searchVersion(navigator.appVersion)
366 || "an unknown version";
367 this.OS = this.searchString(this.dataOS) || "an unknown OS";
369 searchString: function (data) {
370 for (var i=0;i<data.length;i++) {
371 var dataString = data[i].string;
372 var dataProp = data[i].prop;
373 this.versionSearchString = data[i].versionSearch || data[i].identity;
375 if (dataString.indexOf(data[i].subString) != -1)
376 return data[i].identity;
379 return data[i].identity;
382 searchVersion: function (dataString) {
383 var index = dataString.indexOf(this.versionSearchString);
384 if (index == -1) return;
385 return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
389 string: navigator.userAgent,
393 { string: navigator.userAgent,
394 subString: "OmniWeb",
395 versionSearch: "OmniWeb/",
399 string: navigator.vendor,
402 versionSearch: "Version"
407 versionSearch: "Version"
410 string: navigator.vendor,
415 string: navigator.vendor,
417 identity: "Konqueror"
420 string: navigator.userAgent,
421 subString: "Firefox",
425 string: navigator.vendor,
429 { // for newer Netscapes (6+)
430 string: navigator.userAgent,
431 subString: "Netscape",
435 string: navigator.userAgent,
437 identity: "Explorer",
438 versionSearch: "MSIE"
441 string: navigator.userAgent,
446 { // for older Netscapes (4-)
447 string: navigator.userAgent,
448 subString: "Mozilla",
449 identity: "Netscape",
450 versionSearch: "Mozilla"
455 string: navigator.platform,
460 string: navigator.platform,
465 string: navigator.userAgent,
467 identity: "iPhone/iPod"
470 string: navigator.platform,
477 BrowserDetect.init();
479 document.getElementById("brow").textContent = " " + BrowserDetect.browser + " "
480 + BrowserDetect.version +" " + BrowserDetect.OS +" ";
484 function get_appropriate_ws_url()
487 var u = document.URL;
490 * We open the websocket encrypted if this page came on an
491 * https:// url itself, otherwise unencrypted
494 if (u.substring(0, 5) == "https") {
499 if (u.substring(0, 4) == "http")
505 /* + "/xxx" bit is for IE10 workaround */
507 return pcol + u[0] + "/xxx";
511 document.getElementById("number").textContent = get_appropriate_ws_url();
513 /* dumb increment protocol */
517 if (typeof MozWebSocket != "undefined") {
518 socket_di = new MozWebSocket(get_appropriate_ws_url(),
519 "dumb-increment-protocol");
521 socket_di = new WebSocket(get_appropriate_ws_url(),
522 "dumb-increment-protocol");
527 socket_di.onopen = function() {
528 document.getElementById("wsdi_statustd").style.backgroundColor = "#40ff40";
529 document.getElementById("wsdi_status").innerHTML =
530 " <b>websocket connection opened</b><br>" +
531 san(socket_di.extensions);
534 socket_di.onmessage =function got_packet(msg) {
535 document.getElementById("number").textContent = msg.data + "\n";
538 socket_di.onclose = function(){
539 document.getElementById("wsdi_statustd").style.backgroundColor = "#ff4040";
540 document.getElementById("wsdi_status").textContent = " websocket connection CLOSED ";
543 alert('<p>Error' + exception);
546 var socket_status, jso, s;
548 if (typeof MozWebSocket != "undefined") {
549 socket_status = new MozWebSocket(get_appropriate_ws_url(),
552 socket_status = new WebSocket(get_appropriate_ws_url(),
558 socket_status.onopen = function() {
559 document.getElementById("s_statustd").style.backgroundColor = "#40ff40";
560 document.getElementById("s_status").innerHTML =
561 " <b>websocket connection opened</b><br>" +
562 san(socket_status.extensions);
565 socket_status.onmessage =function got_packet(msg) {
566 jso = JSON.parse(msg.data);
568 document.getElementById("servinfo").innerHTML =
569 "<table><tr><td class=l>Build info</td><td>"+
570 san(jso.version) + "</td></tr>" +
571 "<tr><td class=l>Server info</td><td>" +
572 san(jso.hostname) + "</td></tr>" +
576 for (n = 0; n < jso.conns.length; n++)
577 s = s + "<tr><td class=l>client " + (n + 1) +
578 "</td><td><b>" + san(jso.conns[n].peer) +
579 "</b><br>" + san(jso.conns[n].time) +
580 "<br>" + san(jso.conns[n].ua) +
584 document.getElementById("conninfo").innerHTML = s;
587 socket_status.onclose = function(){
588 document.getElementById("s_statustd").style.backgroundColor = "#ff4040";
589 document.getElementById("s_status").textContent = " websocket connection CLOSED ";
592 alert('<p>Error' + exception);
596 socket_di.send("reset\n");
601 for(var word = ''; word.length < 9000; word += 'a'){}
602 socket_di.send(word);
608 if (typeof MozWebSocket != "undefined") {
609 socket_ot = new MozWebSocket(get_appropriate_ws_url(),
610 "dumb-increment-protocol");
612 socket_ot = new WebSocket(get_appropriate_ws_url(),
613 "dumb-increment-protocol");
616 socket_ot.onopen = function() {
617 document.getElementById("ot_statustd").style.backgroundColor = "#40ff40";
618 document.getElementById("ot_status").innerHTML = " <b>websocket connection opened</b><br>" + san(socket_di.extensions);
619 document.getElementById("ot_open_btn").disabled = true;
620 document.getElementById("ot_close_btn").disabled = false;
621 document.getElementById("ot_req_close_btn").disabled = false;
624 socket_ot.onclose = function(e){
625 document.getElementById("ot_statustd").style.backgroundColor = "#ff4040";
626 document.getElementById("ot_status").textContent = " websocket connection CLOSED, code: " + e.code +
627 ", reason: " + e.reason;
628 document.getElementById("ot_open_btn").disabled = false;
629 document.getElementById("ot_close_btn").disabled = true;
630 document.getElementById("ot_req_close_btn").disabled = true;
633 alert('<p>Error' + exception);
637 /* browser will close the ws in a controlled way */
638 function ot_close() {
639 socket_ot.close(3000, "Bye!");
642 /* we ask the server to close the ws in a controlled way */
643 function ot_req_close() {
644 socket_ot.send("closeme\n");
647 /* lws-mirror protocol */
651 var last_x = 0, last_y = 0;
654 var color = "#000000";
656 if (typeof MozWebSocket != "undefined") {
657 socket_lm = new MozWebSocket(get_appropriate_ws_url(),
658 "lws-mirror-protocol");
660 socket_lm = new WebSocket(get_appropriate_ws_url(),
661 "lws-mirror-protocol");
666 socket_lm.onopen = function() {
667 document.getElementById("wslm_statustd").style.backgroundColor = "#40ff40";
668 document.getElementById("wslm_status").innerHTML =
669 " <b>websocket connection opened</b><br>" +
670 san(socket_lm.extensions);
674 socket_lm.onmessage =function got_packet(msg) {
675 j = msg.data.split(';');
677 while (f < j.length - 1) {
680 ctx.strokeStyle = i[1];
682 ctx.moveTo(+(i[2]), +(i[3]));
683 ctx.lineTo(+(i[4]), +(i[5]));
687 ctx.strokeStyle = i[1];
689 ctx.arc(+(i[2]), +(i[3]), +(i[4]), 0, Math.PI*2, true);
697 socket_lm.onclose = function(){
698 document.getElementById("wslm_statustd").style.backgroundColor = "#ff4040";
699 document.getElementById("wslm_status").textContent = " websocket connection CLOSED ";
700 lws_gray_out(true,{'zindex':'499'});
703 alert('<p>Error' + exception);
706 var canvas = document.createElement('canvas');
709 ctx = canvas.getContext("2d");
711 document.getElementById('wslm_drawing').appendChild(canvas);
713 canvas.addEventListener('mousemove', ev_mousemove, false);
714 canvas.addEventListener('mousedown', ev_mousedown, false);
715 canvas.addEventListener('mouseup', ev_mouseup, false);
717 offsetX = offsetY = 0;
719 if (element.offsetParent) {
721 offsetX += element.offsetLeft;
722 offsetY += element.offsetTop;
723 } while ((element = element.offsetParent));
726 function update_color() {
727 color = document.getElementById("color").value;
730 function ev_mousedown (ev) {
734 function ev_mouseup(ev) {
739 function ev_mousemove (ev) {
746 x = ev.layerX - offsetX;
747 y = ev.layerY - offsetY;
759 socket_lm.send("d " + color + " " + last_x + " " + last_y + " " + x + ' ' + y + ';');