| Leo Repp | 58b9f11 | 2021-11-22 11:57:47 +0100 | [diff] [blame^] | 1 | <!doctype html> |
| 2 | <html> |
| 3 | <head> |
| 4 | <meta http-equiv="Content-type" content="text/html; charset=utf-8"> |
| 5 | <title>WebSocket test</title> |
| 6 | </head> |
| 7 | <body> |
| 8 | |
| 9 | <h1>WebSocket test</h1> |
| 10 | <ul></ul> |
| 11 | |
| 12 | <script type="text/javascript"> |
| 13 | var logger = document.getElementsByTagName('ul')[0], |
| 14 | Socket = window.MozWebSocket || window.WebSocket, |
| 15 | protos = ['foo', 'bar', 'xmpp'], |
| 16 | socket = new Socket('ws://' + location.hostname + ':' + location.port + '/', protos), |
| 17 | index = 0; |
| 18 | |
| 19 | var log = function(text) { |
| 20 | logger.innerHTML += '<li>' + text + '</li>'; |
| 21 | }; |
| 22 | |
| 23 | socket.addEventListener('open', function() { |
| 24 | log('OPEN: ' + socket.protocol); |
| 25 | socket.send('Hello, world'); |
| 26 | }); |
| 27 | |
| 28 | socket.onerror = function(event) { |
| 29 | log('ERROR: ' + event.message); |
| 30 | }; |
| 31 | |
| 32 | socket.onmessage = function(event) { |
| 33 | log('MESSAGE: ' + event.data); |
| 34 | setTimeout(function() { socket.send(++index + ' ' + event.data) }, 2000); |
| 35 | }; |
| 36 | |
| 37 | socket.onclose = function(event) { |
| 38 | log('CLOSE: ' + event.code + ', ' + event.reason); |
| 39 | }; |
| 40 | </script> |
| 41 | |
| 42 | </body> |
| 43 | </html> |