websocket.xhtml 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <!DOCTYPE HTML>
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta charset="UTF-8"></meta>
  5. <title>Websocket test</title>
  6. <style type="text/css" media="screen">
  7. body { background:#eee; margin:0 }
  8. .main {
  9. display:block; border:1px solid #ccc; position:absolute;
  10. top:5%; left:5%; width:90%; height:90%; background:#fff;
  11. }
  12. </style>
  13. </head>
  14. <body>
  15. <script type="text/javascript"><![CDATA[
  16. var connection;
  17. var websock_text_field;
  18. var hand_hour;
  19. var hand_min;
  20. function webSockKeepAlive() {
  21. if (keepAlive) {
  22. connection.send('client still alive');
  23. console.log('send keep alive')
  24. setTimeout("webSockKeepAlive()", 10000);
  25. }
  26. }
  27. function load() {
  28. var wsproto = (location.protocol === 'https:') ? "wss:" : "ws:";
  29. connection = new WebSocket(wsproto + "//" + window.location.host + "/websocket.lua");
  30. websock_text_field = document.getElementById('websock_text_field');
  31. hand_min = document.getElementById('hand_min');
  32. hand_hour = document.getElementById('hand_hour');
  33. connection.onopen = function () {
  34. keepAlive = true;
  35. webSockKeepAlive();
  36. };
  37. // Log errors
  38. connection.onerror = function (error) {
  39. keepAlive = false;
  40. alert("WebSocket error");
  41. connection.close();
  42. };
  43. // Log messages from the server
  44. connection.onmessage = function (e) {
  45. var lCmd = e.data.substring(0,3);
  46. if (lCmd == "-->") {
  47. console.log(e.data);
  48. var lDirection = Number(e.data.substring(5));
  49. if (e.data[3] == 'h') {
  50. hand_hour.setAttribute("transform", "rotate(" + lDirection + " 800 600)");
  51. }
  52. if (e.data[3] == 'm') {
  53. hand_min.setAttribute("transform", "rotate(" + lDirection + " 800 600)");
  54. }
  55. } else {
  56. websock_text_field.textContent = e.data;
  57. }
  58. };
  59. console.log("load");
  60. }
  61. ]]></script>
  62. <svg class="main"
  63. xmlns="http://www.w3.org/2000/svg"
  64. xmlns:svg="http://www.w3.org/2000/svg"
  65. version="1.1"
  66. xmlns:xlink="http://www.w3.org/1999/xlink"
  67. viewBox="0 0 1600 1200" preserveAspectRatio="xMinYMin meet"
  68. onload="load()"
  69. >
  70. <circle id="line_a" cx="800" cy="600" r="500" style="stroke:rgb(255,0,0); stroke-width:5; fill:rgb(200,200,200)"/>
  71. <polygon points="800,200 900,300 850,300 850,600 750,600 750,300 700,300" style="fill:rgb(100,0,0)" transform="rotate(0,800,600)" id="hand_hour"/>
  72. <polygon points="800,100 840,200 820,200 820,600 780,600 780,200 760,200" style="fill:rgb(0,100,0)" transform="rotate(0,800,600)" id="hand_min"/>
  73. <text id="websock_text_field" x="800" y="600" text-anchor="middle" font-size="50px" fill="red">No websocket connection yet</text>
  74. </svg>
  75. </body>
  76. </html>