5 <title>Minimal Websocket test app</title>
9 <table><tr><td><img src="/libwebsockets.org-logo.png"></td><td>
10 <h3>Detected Browser: <div id=brow>...</div></h3></td></tr></table>
11 <h2>libwebsockets "dumb-increment-protocol" test applet</h2>
12 The incrementing number is coming from the server and is individual for
13 each connection to the server... try opening a second browser window.
14 Click the button to send the server a websocket message to
15 reset the number.<br><br>
19 <td align=center><input type=button id=offset value="Reset counter" onclick="reset();" ></td>
20 <td width=100 align=center><div id=number> </div></td>
21 <td id=wsdi_statustd align=center><div id=wsdi_status>Not initialized</div></td>
25 <h2>libwebsockets "lws-mirror-protocol" test applet</h2>
26 Use the mouse to draw on the canvas below -- all other browser windows open
27 on this page see your drawing in realtime and you can see any of theirs as
30 The lws-mirror protocol doesn't interpret what is being sent to it, it just
31 re-sends it to every other websocket it has a connection with using that
32 protocol, including the guy who sent the packet.
33 <p>libwebsockets-test-client spams circles on to this shared canvas when
40 <select id="color" onchange="update_color();">
41 <option value=#000000>Black</option>
42 <option value=#0000ff>Blue</option>
43 <option value=#20ff20>Green</option>
44 <option value=#802020>Dark Red</option>
47 <td id=wslm_statustd align=center><div id=wslm_status>Not initialized</div></td>
50 <td colspan=2 width=500 align=center style="background-color: #e0e0e0;"><div id=wslm_drawing> </div></td>
56 /* BrowserDetect came from http://www.quirksmode.org/js/detect.html */
60 this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
61 this.version = this.searchVersion(navigator.userAgent)
62 || this.searchVersion(navigator.appVersion)
63 || "an unknown version";
64 this.OS = this.searchString(this.dataOS) || "an unknown OS";
66 searchString: function (data) {
67 for (var i=0;i<data.length;i++) {
68 var dataString = data[i].string;
69 var dataProp = data[i].prop;
70 this.versionSearchString = data[i].versionSearch || data[i].identity;
72 if (dataString.indexOf(data[i].subString) != -1)
73 return data[i].identity;
76 return data[i].identity;
79 searchVersion: function (dataString) {
80 var index = dataString.indexOf(this.versionSearchString);
81 if (index == -1) return;
82 return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
86 string: navigator.userAgent,
90 { string: navigator.userAgent,
92 versionSearch: "OmniWeb/",
96 string: navigator.vendor,
99 versionSearch: "Version"
104 versionSearch: "Version"
107 string: navigator.vendor,
112 string: navigator.vendor,
114 identity: "Konqueror"
117 string: navigator.userAgent,
118 subString: "Firefox",
122 string: navigator.vendor,
126 { // for newer Netscapes (6+)
127 string: navigator.userAgent,
128 subString: "Netscape",
132 string: navigator.userAgent,
134 identity: "Explorer",
135 versionSearch: "MSIE"
138 string: navigator.userAgent,
143 { // for older Netscapes (4-)
144 string: navigator.userAgent,
145 subString: "Mozilla",
146 identity: "Netscape",
147 versionSearch: "Mozilla"
152 string: navigator.platform,
157 string: navigator.platform,
162 string: navigator.userAgent,
164 identity: "iPhone/iPod"
167 string: navigator.platform,
174 BrowserDetect.init();
176 document.getElementById("brow").textContent = " " + BrowserDetect.browser + " "
177 + BrowserDetect.version +" " + BrowserDetect.OS +" ";
181 function get_appropriate_ws_url()
184 var u = document.URL;
187 * We open the websocket encrypted if this page came on an
188 * https:// url itself, otherwise unencrypted
191 if (u.substring(0, 5) == "https") {
196 if (u.substring(0, 4) == "http")
206 document.getElementById("number").textContent = get_appropriate_ws_url();
208 /* dumb increment protocol */
212 if (typeof MozWebSocket != "undefined") {
213 socket_di = new MozWebSocket(get_appropriate_ws_url(),
214 "dumb-increment-protocol");
216 socket_di = new WebSocket(get_appropriate_ws_url(),
217 "dumb-increment-protocol");
222 socket_di.onopen = function() {
223 document.getElementById("wsdi_statustd").style.backgroundColor = "#40ff40";
224 document.getElementById("wsdi_status").textContent = " websocket connection opened ";
227 socket_di.onmessage =function got_packet(msg) {
228 document.getElementById("number").textContent = msg.data + "\n";
231 socket_di.onclose = function(){
232 document.getElementById("wsdi_statustd").style.backgroundColor = "#ff4040";
233 document.getElementById("wsdi_status").textContent = " websocket connection CLOSED ";
236 alert('<p>Error' + exception);
240 socket_di.send("reset\n");
244 /* lws-mirror protocol */
248 var last_x = 0, last_y = 0;
251 var color = "#000000";
253 if (typeof MozWebSocket != "undefined") {
254 socket_lm = new MozWebSocket(get_appropriate_ws_url(),
255 "lws-mirror-protocol");
257 socket_lm = new WebSocket(get_appropriate_ws_url(),
258 "lws-mirror-protocol");
263 socket_lm.onopen = function() {
264 document.getElementById("wslm_statustd").style.backgroundColor = "#40ff40";
265 document.getElementById("wslm_status").textContent = " websocket connection opened ";
268 socket_lm.onmessage =function got_packet(msg) {
269 j = msg.data.split(';');
271 while (f < j.length - 1) {
274 ctx.strokeStyle = i[1];
276 ctx.moveTo(+(i[2]), +(i[3]));
277 ctx.lineTo(+(i[4]), +(i[5]));
281 ctx.strokeStyle = i[1];
283 ctx.arc(+(i[2]), +(i[3]), +(i[4]), 0, Math.PI*2, true);
291 socket_lm.onclose = function(){
292 document.getElementById("wslm_statustd").style.backgroundColor = "#ff4040";
293 document.getElementById("wslm_status").textContent = " websocket connection CLOSED ";
296 alert('<p>Error' + exception);
299 var canvas = document.createElement('canvas');
302 ctx = canvas.getContext("2d");
304 document.getElementById('wslm_drawing').appendChild(canvas);
306 canvas.addEventListener('mousemove', ev_mousemove, false);
307 canvas.addEventListener('mousedown', ev_mousedown, false);
308 canvas.addEventListener('mouseup', ev_mouseup, false);
310 offsetX = offsetY = 0;
312 if (element.offsetParent) {
314 offsetX += element.offsetLeft;
315 offsetY += element.offsetTop;
316 } while ((element = element.offsetParent));
319 function update_color() {
320 color = document.getElementById("color").value;
323 function ev_mousedown (ev) {
327 function ev_mouseup(ev) {
332 function ev_mousemove (ev) {
339 x = ev.layerX - offsetX;
340 y = ev.layerY - offsetY;
352 socket_lm.send("d " + color + " " + last_x + " " + last_y + " " + x + ' ' + y + ';');