websocket.html 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. <html>
  2. <head>
  3. <style>
  4. #rx {
  5. font-family: 'Courier New', Courier, monospace;
  6. font-size: small;
  7. }
  8. </style>
  9. <script type="text/javascript" src="common.js?v=$COMMIT_HASH"></script>
  10. </head>
  11. <body>
  12. <div id="rx"></div>
  13. <script>
  14. var gateway = getDomainname().replace("http://", "ws://") + "/ws";
  15. var websocket;
  16. window.addEventListener('load', onLoad);
  17. function initWebSocket() {
  18. console.log('Trying to open a WebSocket connection...');
  19. addToLog('Trying to open a WebSocket connection...');
  20. websocket = new WebSocket(gateway);
  21. websocket.onopen = onOpen;
  22. websocket.onclose = onClose;
  23. websocket.onmessage = onMessage; // <-- add this line
  24. }
  25. function onOpen(event) {
  26. console.log('Connection opened');
  27. addToLog('Connection opened');
  28. }
  29. function onClose(event) {
  30. console.log('Connection closed');
  31. addToLog('Connection closed');
  32. setTimeout(initWebSocket, 2000);
  33. }
  34. function onMessage(event) {
  35. console.log(event);
  36. addToLog(event.data);
  37. }
  38. function onLoad(event) {
  39. initWebSocket();
  40. }
  41. function addToLog(msg) {
  42. document.getElementById('rx').innerHTML += "[" + new Date().toLocaleTimeString() + "] " +msg + "<br>\n";
  43. window.scrollBy(0,document.body.scrollHeight);
  44. }
  45. </script>
  46. </body>
  47. </html>