add by hand http send example
[platform/upstream/libwebsockets.git] / test-server / test.html
1 <!DOCTYPE html>
2 <html lang="en">
3 <head>
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; }
13 </style>
14 </head>
15
16 <body>
17 <header></header>
18 <article>
19
20 <table><tr><td>
21
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>
24
25 </td></tr><tr><td>
26
27 <section id="increment" class="group2">
28 <div class="title">libwebsockets "dumb-increment-protocol"</div>
29 <table><tr><td>
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>
34         </tr>
35 </table>
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.
39 <br/><br/>
40 The button zeros just this connection's number.
41 <br/><br/>
42 Click <a href="/leaf.jpg" target="_blank">Here</a> to have the test server send a big picture by http.
43 </td></tr></table>
44 </section>
45 <br>
46 <section id="mirror" class="group2">
47 <div class="title">libwebsockets "lws-mirror-protocol"</div>
48 <div class="explain">
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
51 well.
52 <br/><br/>
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.
56 <br/><br/>
57 <b>libwebsockets-test-client</b> joins in by spamming circles on to this shared canvas when
58 run.
59 </div>
60 <table class="content">
61         <tr>
62                 <td>Drawing color:
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>
68                 </select>
69                 </td>
70                 <td id=wslm_statustd align=center class="explain"><div id=wslm_status>Not initialized</div></td>
71         </tr>
72         <tr>
73                 <td colspan=2 width=500 class="content">
74                 <div id="wslm_drawing">
75                 </div></td>
76         </tr>
77 </table>
78 </section>
79
80 </td></tr><tr><td>
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>
83
84 </td></tr></table>
85
86 </article>
87
88 <script>
89
90 /* BrowserDetect came from http://www.quirksmode.org/js/detect.html */
91
92 var BrowserDetect = {
93         init: function () {
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";
99         },
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;
105                         if (dataString) {
106                                 if (dataString.indexOf(data[i].subString) != -1)
107                                         return data[i].identity;
108                         }
109                         else if (dataProp)
110                                 return data[i].identity;
111                 }
112         },
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));
117         },
118         dataBrowser: [
119                 {
120                         string: navigator.userAgent,
121                         subString: "Chrome",
122                         identity: "Chrome"
123                 },
124                 {       string: navigator.userAgent,
125                         subString: "OmniWeb",
126                         versionSearch: "OmniWeb/",
127                         identity: "OmniWeb"
128                 },
129                 {
130                         string: navigator.vendor,
131                         subString: "Apple",
132                         identity: "Safari",
133                         versionSearch: "Version"
134                 },
135                 {
136                         prop: window.opera,
137                         identity: "Opera",
138                         versionSearch: "Version"
139                 },
140                 {
141                         string: navigator.vendor,
142                         subString: "iCab",
143                         identity: "iCab"
144                 },
145                 {
146                         string: navigator.vendor,
147                         subString: "KDE",
148                         identity: "Konqueror"
149                 },
150                 {
151                         string: navigator.userAgent,
152                         subString: "Firefox",
153                         identity: "Firefox"
154                 },
155                 {
156                         string: navigator.vendor,
157                         subString: "Camino",
158                         identity: "Camino"
159                 },
160                 {               // for newer Netscapes (6+)
161                         string: navigator.userAgent,
162                         subString: "Netscape",
163                         identity: "Netscape"
164                 },
165                 {
166                         string: navigator.userAgent,
167                         subString: "MSIE",
168                         identity: "Explorer",
169                         versionSearch: "MSIE"
170                 },
171                 {
172                         string: navigator.userAgent,
173                         subString: "Gecko",
174                         identity: "Mozilla",
175                         versionSearch: "rv"
176                 },
177                 {               // for older Netscapes (4-)
178                         string: navigator.userAgent,
179                         subString: "Mozilla",
180                         identity: "Netscape",
181                         versionSearch: "Mozilla"
182                 }
183         ],
184         dataOS : [
185                 {
186                         string: navigator.platform,
187                         subString: "Win",
188                         identity: "Windows"
189                 },
190                 {
191                         string: navigator.platform,
192                         subString: "Mac",
193                         identity: "Mac"
194                 },
195                 {
196                            string: navigator.userAgent,
197                            subString: "iPhone",
198                            identity: "iPhone/iPod"
199             },
200                 {
201                         string: navigator.platform,
202                         subString: "Linux",
203                         identity: "Linux"
204                 }
205         ]
206
207 };
208 BrowserDetect.init();
209
210 document.getElementById("brow").textContent = " " + BrowserDetect.browser + " "
211         + BrowserDetect.version +" " + BrowserDetect.OS +" ";
212
213         var pos = 0;
214
215 function get_appropriate_ws_url()
216 {
217         var pcol;
218         var u = document.URL;
219
220         /*
221          * We open the websocket encrypted if this page came on an
222          * https:// url itself, otherwise unencrypted
223          */
224
225         if (u.substring(0, 5) == "https") {
226                 pcol = "wss://";
227                 u = u.substr(8);
228         } else {
229                 pcol = "ws://";
230                 if (u.substring(0, 4) == "http")
231                         u = u.substr(7);
232         }
233
234         u = u.split('/');
235
236         return pcol + u[0];
237 }
238
239
240 document.getElementById("number").textContent = get_appropriate_ws_url();
241
242 /* dumb increment protocol */
243         
244         var socket_di;
245
246         if (typeof MozWebSocket != "undefined") {
247                 socket_di = new MozWebSocket(get_appropriate_ws_url(),
248                                    "dumb-increment-protocol");
249         } else {
250                 socket_di = new WebSocket(get_appropriate_ws_url(),
251                                    "dumb-increment-protocol");
252         }
253
254
255         try {
256                 socket_di.onopen = function() {
257                         document.getElementById("wsdi_statustd").style.backgroundColor = "#40ff40";
258                         document.getElementById("wsdi_status").textContent = " websocket connection opened ";
259                 } 
260
261                 socket_di.onmessage =function got_packet(msg) {
262                         document.getElementById("number").textContent = msg.data + "\n";
263                 } 
264
265                 socket_di.onclose = function(){
266                         document.getElementById("wsdi_statustd").style.backgroundColor = "#ff4040";
267                         document.getElementById("wsdi_status").textContent = " websocket connection CLOSED ";
268                 }
269         } catch(exception) {
270                 alert('<p>Error' + exception);  
271         }
272
273 function reset() {
274         socket_di.send("reset\n");
275 }
276
277
278 /* lws-mirror protocol */
279
280         var down = 0;
281         var no_last = 1;
282         var last_x = 0, last_y = 0;
283         var ctx;
284         var socket_lm;
285         var color = "#000000";
286
287         if (typeof MozWebSocket != "undefined") {
288                 socket_lm = new MozWebSocket(get_appropriate_ws_url(),
289                                    "lws-mirror-protocol");
290         } else {
291                 socket_lm = new WebSocket(get_appropriate_ws_url(),
292                                    "lws-mirror-protocol");
293         }
294
295
296         try {
297                 socket_lm.onopen = function() {
298                         document.getElementById("wslm_statustd").style.backgroundColor = "#40ff40";
299                         document.getElementById("wslm_status").textContent = " websocket connection opened ";
300                 } 
301
302                 socket_lm.onmessage =function got_packet(msg) {
303                         j = msg.data.split(';');
304                         f = 0;
305                         while (f < j.length - 1) {
306                                 i = j[f].split(' ');
307                                 if (i[0] == 'd') {
308                                         ctx.strokeStyle = i[1];
309                                         ctx.beginPath();
310                                         ctx.moveTo(+(i[2]), +(i[3]));
311                                         ctx.lineTo(+(i[4]), +(i[5]));
312                                         ctx.stroke();
313                                 }
314                                 if (i[0] == 'c') {
315                                         ctx.strokeStyle = i[1];
316                                         ctx.beginPath();
317                                         ctx.arc(+(i[2]), +(i[3]), +(i[4]), 0, Math.PI*2, true); 
318                                         ctx.stroke();
319                                 }
320
321                                 f++;
322                         }
323                 }
324
325                 socket_lm.onclose = function(){
326                         document.getElementById("wslm_statustd").style.backgroundColor = "#ff4040";
327                         document.getElementById("wslm_status").textContent = " websocket connection CLOSED ";
328                 }
329         } catch(exception) {
330                 alert('<p>Error' + exception);  
331         }
332
333         var canvas = document.createElement('canvas');
334         canvas.height = 300;
335         canvas.width = 480;
336         ctx = canvas.getContext("2d");
337
338         document.getElementById('wslm_drawing').appendChild(canvas);
339
340         canvas.addEventListener('mousemove', ev_mousemove, false);
341         canvas.addEventListener('mousedown', ev_mousedown, false);
342         canvas.addEventListener('mouseup', ev_mouseup, false);
343
344         offsetX = offsetY = 0;
345         element = canvas;
346       if (element.offsetParent) {
347         do {
348           offsetX += element.offsetLeft;
349           offsetY += element.offsetTop;
350         } while ((element = element.offsetParent));
351       }
352  
353 function update_color() {
354         color = document.getElementById("color").value;
355 }
356
357 function ev_mousedown (ev) {
358         down = 1;
359 }
360
361 function ev_mouseup(ev) {
362         down = 0;
363         no_last = 1;
364 }
365
366 function ev_mousemove (ev) {
367         var x, y;
368
369         if (ev.offsetX) {
370                 x = ev.offsetX;
371                 y = ev.offsetY;
372         } else {
373                 x = ev.layerX - offsetX;
374                 y = ev.layerY - offsetY;
375
376         }
377
378         if (!down)
379                 return;
380         if (no_last) {
381                 no_last = 0;
382                 last_x = x;
383                 last_y = y;
384                 return;
385         }
386         socket_lm.send("d " + color + " " + last_x + " " + last_y + " " + x + ' ' + y + ';');
387
388         last_x = x;
389         last_y = y;
390 }
391
392
393 </script>
394
395 </body>
396 </html>