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">
81 Looking for support? <a href="http://libwebsockets.org">http://libwebsockets.org</a><br/>
82 Join the mailing list: <a href="http://ml.libwebsockets.org/mailman/listinfo/libwebsockets">http://ml.libwebsockets.org/mailman/listinfo/libwebsockets</a>
90 /* BrowserDetect came from http://www.quirksmode.org/js/detect.html */
94 this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
95 this.version = this.searchVersion(navigator.userAgent)
96 || this.searchVersion(navigator.appVersion)
97 || "an unknown version";
98 this.OS = this.searchString(this.dataOS) || "an unknown OS";
100 searchString: function (data) {
101 for (var i=0;i<data.length;i++) {
102 var dataString = data[i].string;
103 var dataProp = data[i].prop;
104 this.versionSearchString = data[i].versionSearch || data[i].identity;
106 if (dataString.indexOf(data[i].subString) != -1)
107 return data[i].identity;
110 return data[i].identity;
113 searchVersion: function (dataString) {
114 var index = dataString.indexOf(this.versionSearchString);
115 if (index == -1) return;
116 return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
120 string: navigator.userAgent,
124 { string: navigator.userAgent,
125 subString: "OmniWeb",
126 versionSearch: "OmniWeb/",
130 string: navigator.vendor,
133 versionSearch: "Version"
138 versionSearch: "Version"
141 string: navigator.vendor,
146 string: navigator.vendor,
148 identity: "Konqueror"
151 string: navigator.userAgent,
152 subString: "Firefox",
156 string: navigator.vendor,
160 { // for newer Netscapes (6+)
161 string: navigator.userAgent,
162 subString: "Netscape",
166 string: navigator.userAgent,
168 identity: "Explorer",
169 versionSearch: "MSIE"
172 string: navigator.userAgent,
177 { // for older Netscapes (4-)
178 string: navigator.userAgent,
179 subString: "Mozilla",
180 identity: "Netscape",
181 versionSearch: "Mozilla"
186 string: navigator.platform,
191 string: navigator.platform,
196 string: navigator.userAgent,
198 identity: "iPhone/iPod"
201 string: navigator.platform,
208 BrowserDetect.init();
210 document.getElementById("brow").textContent = " " + BrowserDetect.browser + " "
211 + BrowserDetect.version +" " + BrowserDetect.OS +" ";
215 function get_appropriate_ws_url()
218 var u = document.URL;
221 * We open the websocket encrypted if this page came on an
222 * https:// url itself, otherwise unencrypted
225 if (u.substring(0, 5) == "https") {
230 if (u.substring(0, 4) == "http")
240 document.getElementById("number").textContent = get_appropriate_ws_url();
242 /* dumb increment protocol */
246 if (typeof MozWebSocket != "undefined") {
247 socket_di = new MozWebSocket(get_appropriate_ws_url(),
248 "dumb-increment-protocol");
250 socket_di = new WebSocket(get_appropriate_ws_url(),
251 "dumb-increment-protocol");
256 socket_di.onopen = function() {
257 document.getElementById("wsdi_statustd").style.backgroundColor = "#40ff40";
258 document.getElementById("wsdi_status").textContent = " websocket connection opened ";
261 socket_di.onmessage =function got_packet(msg) {
262 document.getElementById("number").textContent = msg.data + "\n";
265 socket_di.onclose = function(){
266 document.getElementById("wsdi_statustd").style.backgroundColor = "#ff4040";
267 document.getElementById("wsdi_status").textContent = " websocket connection CLOSED ";
270 alert('<p>Error' + exception);
274 socket_di.send("reset\n");
278 /* lws-mirror protocol */
282 var last_x = 0, last_y = 0;
285 var color = "#000000";
287 if (typeof MozWebSocket != "undefined") {
288 socket_lm = new MozWebSocket(get_appropriate_ws_url(),
289 "lws-mirror-protocol");
291 socket_lm = new WebSocket(get_appropriate_ws_url(),
292 "lws-mirror-protocol");
297 socket_lm.onopen = function() {
298 document.getElementById("wslm_statustd").style.backgroundColor = "#40ff40";
299 document.getElementById("wslm_status").textContent = " websocket connection opened ";
302 socket_lm.onmessage =function got_packet(msg) {
303 j = msg.data.split(';');
305 while (f < j.length - 1) {
308 ctx.strokeStyle = i[1];
310 ctx.moveTo(+(i[2]), +(i[3]));
311 ctx.lineTo(+(i[4]), +(i[5]));
315 ctx.strokeStyle = i[1];
317 ctx.arc(+(i[2]), +(i[3]), +(i[4]), 0, Math.PI*2, true);
325 socket_lm.onclose = function(){
326 document.getElementById("wslm_statustd").style.backgroundColor = "#ff4040";
327 document.getElementById("wslm_status").textContent = " websocket connection CLOSED ";
330 alert('<p>Error' + exception);
333 var canvas = document.createElement('canvas');
336 ctx = canvas.getContext("2d");
338 document.getElementById('wslm_drawing').appendChild(canvas);
340 canvas.addEventListener('mousemove', ev_mousemove, false);
341 canvas.addEventListener('mousedown', ev_mousedown, false);
342 canvas.addEventListener('mouseup', ev_mouseup, false);
344 offsetX = offsetY = 0;
346 if (element.offsetParent) {
348 offsetX += element.offsetLeft;
349 offsetY += element.offsetTop;
350 } while ((element = element.offsetParent));
353 function update_color() {
354 color = document.getElementById("color").value;
357 function ev_mousedown (ev) {
361 function ev_mouseup(ev) {
366 function ev_mousemove (ev) {
373 x = ev.layerX - offsetX;
374 y = ev.layerY - offsetY;
386 socket_lm.send("d " + color + " " + last_x + " " + last_y + " " + x + ' ' + y + ';');