Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 1 | <!DOCTYPE html> |
| 2 | <html lang="en"> |
| 3 | <head> |
| 4 | <meta charset=utf-8 /> |
Andy Green | ab7d933 | 2010-11-11 13:19:19 +0000 | [diff] [blame] | 5 | <title>Minimal Websocket test app</title> |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 6 | </head> |
| 7 | |
| 8 | <body> |
Andy Green | ab7d933 | 2010-11-11 13:19:19 +0000 | [diff] [blame] | 9 | <h2>libwebsockets "dumb-increment-protocol" test applet</h2> |
| 10 | The incrementing number is coming from the server. |
| 11 | Click the button to send the server a websocket message to |
| 12 | reset the number.<br><br> |
| 13 | |
| 14 | <table> |
| 15 | <tr> |
| 16 | <td align=center><input type=button id=offset value="Reset counter" onclick="reset();" ></td> |
| 17 | <td width=100 align=center><div id=number> </div></td> |
Andy Green | fe2a0d2 | 2010-11-12 13:10:40 +0000 | [diff] [blame^] | 18 | <td id=wsdi_statustd align=center><div id=wsdi_status>Not initialized</div></td> |
| 19 | </tr> |
| 20 | </table> |
| 21 | |
| 22 | <h2>libwebsockets "lws-mirror-protocol" test applet</h2> |
| 23 | Use the mouse to draw on the canvas below -- all other browser windows open |
| 24 | on this page see your drawing in realtime and you can see any of theirs as |
| 25 | well. |
| 26 | <p> |
| 27 | The lws-mirror protocol doesn't interpret what is being sent to it, it just |
| 28 | re-sends it to every other websocket it has a connection with using that |
| 29 | protocol, including the guy who sent the packet. |
| 30 | <br><br> |
| 31 | |
| 32 | <table> |
| 33 | <tr> |
| 34 | <td>Drawing color: |
| 35 | <select id="color" onchange="update_color();"> |
| 36 | <option value=#000000>Black</option> |
| 37 | <option value=#0000ff>Blue</option> |
| 38 | <option value=#20ff20>Green</option> |
| 39 | <option value=#802020>Dark Red</option> |
| 40 | </select> |
| 41 | </td> |
| 42 | <td id=wslm_statustd align=center><div id=wslm_status>Not initialized</div></td> |
| 43 | </tr> |
| 44 | <tr> |
| 45 | <td colspan=2 width=500 align=center style="background-color: #e0e0e0;"><div id=wslm_drawing> </div></td> |
Andy Green | ab7d933 | 2010-11-11 13:19:19 +0000 | [diff] [blame] | 46 | </tr> |
| 47 | </table> |
| 48 | |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 49 | <script> |
| 50 | var pos = 0; |
| 51 | |
Andy Green | fe2a0d2 | 2010-11-12 13:10:40 +0000 | [diff] [blame^] | 52 | function get_appropriate_ws_url(ads) |
| 53 | { |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 54 | /* |
| 55 | * We open the websocket encrypted if this page came on an |
| 56 | * https:// url itself, otherwise unencrypted |
| 57 | */ |
| 58 | |
| 59 | if (document.URL.substring(0, 5) == "https") |
Andy Green | fe2a0d2 | 2010-11-12 13:10:40 +0000 | [diff] [blame^] | 60 | return "wss://"+ads; |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 61 | else |
Andy Green | fe2a0d2 | 2010-11-12 13:10:40 +0000 | [diff] [blame^] | 62 | return "ws://"+ads; |
| 63 | } |
| 64 | |
| 65 | /* dumb increment protocol */ |
Andy Green | 3faa9c7 | 2010-11-08 17:03:03 +0000 | [diff] [blame] | 66 | |
Andy Green | fe2a0d2 | 2010-11-12 13:10:40 +0000 | [diff] [blame^] | 67 | var socket_di = new WebSocket(get_appropriate_ws_url("127.0.0.1:7681"), |
| 68 | "dumb-increment-protocol"); |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 69 | |
| 70 | try { |
Andy Green | fe2a0d2 | 2010-11-12 13:10:40 +0000 | [diff] [blame^] | 71 | socket_di.onopen = function() { |
| 72 | wsdi_statustd.style.backgroundColor = "#40ff40"; |
| 73 | wsdi_status.textContent = " websocket connection opened "; |
Andy Green | ab7d933 | 2010-11-11 13:19:19 +0000 | [diff] [blame] | 74 | } |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 75 | |
Andy Green | fe2a0d2 | 2010-11-12 13:10:40 +0000 | [diff] [blame^] | 76 | socket_di.onmessage =function got_packet(msg) { |
Andy Green | ab7d933 | 2010-11-11 13:19:19 +0000 | [diff] [blame] | 77 | number.textContent = msg.data + "\n"; |
| 78 | } |
| 79 | |
Andy Green | fe2a0d2 | 2010-11-12 13:10:40 +0000 | [diff] [blame^] | 80 | socket_di.onclose = function(){ |
| 81 | wsdi_statustd.style.backgroundColor = "#ff4040"; |
| 82 | wsdi_status.textContent = " websocket connection closed "; |
Andy Green | ab7d933 | 2010-11-11 13:19:19 +0000 | [diff] [blame] | 83 | } |
| 84 | } catch(exception) { |
Andy Green | fe2a0d2 | 2010-11-12 13:10:40 +0000 | [diff] [blame^] | 85 | alert('<p>Error' + exception); |
Andy Green | ab7d933 | 2010-11-11 13:19:19 +0000 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | function reset() { |
Andy Green | fe2a0d2 | 2010-11-12 13:10:40 +0000 | [diff] [blame^] | 89 | socket_di.send("reset\n"); |
Andy Green | ab7d933 | 2010-11-11 13:19:19 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Andy Green | fe2a0d2 | 2010-11-12 13:10:40 +0000 | [diff] [blame^] | 92 | |
| 93 | /* lws-mirror protocol */ |
| 94 | |
| 95 | var down = 0; |
| 96 | var no_last = 1; |
| 97 | var last_x, last_y; |
| 98 | var ctx; |
| 99 | var socket_lm = new WebSocket(get_appropriate_ws_url("127.0.0.1:7681"), |
| 100 | "lws-mirror-protocol"); |
| 101 | var color = "#000000"; |
| 102 | |
| 103 | try { |
| 104 | socket_lm.onopen = function() { |
| 105 | wslm_statustd.style.backgroundColor = "#40ff40"; |
| 106 | wslm_status.textContent = " websocket connection opened "; |
| 107 | } |
| 108 | |
| 109 | socket_lm.onmessage =function got_packet(msg) { |
| 110 | i = msg.data.split(' '); |
| 111 | if (i[0] == 'd') { |
| 112 | ctx.strokeStyle = i[1]; |
| 113 | ctx.beginPath(); |
| 114 | ctx.moveTo(i[2], i[3]); |
| 115 | ctx.lineTo(i[4], i[5]); |
| 116 | ctx.stroke(); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | socket_lm.onclose = function(){ |
| 121 | wslm_statustd.style.backgroundColor = "#ff4040"; |
| 122 | wslm_status.textContent = " websocket connection closed "; |
| 123 | } |
| 124 | } catch(exception) { |
| 125 | alert('<p>Error' + exception); |
| 126 | } |
| 127 | |
| 128 | var canvas = document.createElement('canvas'); |
| 129 | canvas.height = 300; |
| 130 | canvas.width = 480; |
| 131 | ctx = canvas.getContext("2d"); |
| 132 | |
| 133 | document.getElementById('wslm_drawing').appendChild(canvas); |
| 134 | |
| 135 | canvas.addEventListener('mousemove', ev_mousemove, false); |
| 136 | canvas.addEventListener('mousedown', ev_mousedown, false); |
| 137 | canvas.addEventListener('mouseup', ev_mouseup, false); |
| 138 | |
| 139 | function update_color() { |
| 140 | color = document.getElementById("color").value; |
| 141 | } |
| 142 | |
| 143 | function ev_mousedown (ev) { |
| 144 | down = 1; |
| 145 | } |
| 146 | |
| 147 | function ev_mouseup(ev) { |
| 148 | down = 0; |
| 149 | no_last = 1; |
| 150 | } |
| 151 | |
| 152 | function ev_mousemove (ev) { |
| 153 | var x, y; |
| 154 | |
| 155 | x = ev.offsetX; |
| 156 | y = ev.offsetY; |
| 157 | |
| 158 | if (!down) |
| 159 | return; |
| 160 | if (no_last) { |
| 161 | no_last = 0; |
| 162 | last_x = x; |
| 163 | last_y = y; |
| 164 | return; |
| 165 | } |
| 166 | socket_lm.send("d " + color + " " + last_x + " " + last_y + " " + x + ' ' + y); |
| 167 | |
| 168 | last_x = x; |
| 169 | last_y = y; |
| 170 | } |
| 171 | |
| 172 | |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 173 | </script> |
| 174 | |
| 175 | </body> |
| 176 | </html> |