Thomas Vachuska | 7d638d3 | 2014-11-07 10:24:43 -0800 | [diff] [blame] | 1 | <html> |
| 2 | <head> |
| 3 | <title>Web Sockets Demo</title> |
| 4 | |
| 5 | <script src="libs/jquery-2.1.1.min.js"></script> |
| 6 | |
| 7 | <script type='text/javascript'> |
| 8 | if (!window.WebSocket) |
| 9 | alert("WebSocket not supported by this browser"); |
| 10 | |
| 11 | var server = { |
| 12 | connect : function() { |
| 13 | var location = document.location.toString().replace('http://', |
| 14 | 'ws://').replace('https://', 'wss://').replace('ws.html','ws/topology'); |
| 15 | this.ws = new WebSocket(location); |
| 16 | |
| 17 | this.ws.onopen = function() { |
| 18 | server._send("Hi there!"); |
| 19 | }; |
| 20 | |
| 21 | this.ws.onmessage = function(m) { |
| 22 | if (m.data) { |
| 23 | $('#log').append(m.data).append($('<br/>')); |
| 24 | } |
| 25 | }; |
| 26 | |
| 27 | this.ws.onclose = function(m) { |
| 28 | this.ws = null; |
| 29 | }; |
| 30 | }, |
| 31 | |
| 32 | _send : function(message) { |
| 33 | if (this.ws) { |
| 34 | this.ws.send(message); |
| 35 | } |
| 36 | }, |
| 37 | |
| 38 | send : function(text) { |
| 39 | if (text != null && text.length > 0) { |
| 40 | server._send(text); |
| 41 | } |
| 42 | } |
| 43 | }; |
| 44 | </script> |
| 45 | </head> |
| 46 | <body> |
| 47 | <pre id='log'></pre> |
| 48 | |
| 49 | <script type='text/javascript'> |
| 50 | server.connect(); |
| 51 | </script> |
| 52 | |
| 53 | </body> |
| 54 | </html> |