websocket.xhtml 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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 queryStringElem(name, idx) {
  21. if (typeof(queryStringElem_Table) != "object") {
  22. queryStringElem_Table = {};
  23. window.location.search.slice(1).split('&').forEach(
  24. function(keyValuePair) {
  25. keyValuePair = keyValuePair.split('=');
  26. if (typeof(queryStringElem_Table[keyValuePair[0]]) != "object") {
  27. queryStringElem_Table[keyValuePair[0]] = [];
  28. }
  29. var idx = queryStringElem_Table[keyValuePair[0]].length+1;
  30. queryStringElem_Table[keyValuePair[0]][idx] = keyValuePair[1] || '';
  31. }
  32. );
  33. }
  34. idx = idx || 1;
  35. if (queryStringElem_Table[name]) {
  36. return queryStringElem_Table[name][idx];
  37. }
  38. return null;
  39. }
  40. function webSockKeepAlive() {
  41. if (keepAlive) {
  42. connection.send('client still alive');
  43. console.log('send keep alive')
  44. setTimeout("webSockKeepAlive()", 10000);
  45. }
  46. }
  47. function load() {
  48. var wsproto = (location.protocol === 'https:') ? "wss:" : "ws:";
  49. connection = new WebSocket(wsproto + "//" + window.location.host + "/websocket.lua");
  50. websock_text_field = document.getElementById('websock_text_field');
  51. hand_min = document.getElementById('hand_min');
  52. hand_hour = document.getElementById('hand_hour');
  53. var ka = queryStringElem("keepAlive");
  54. if (ka) {
  55. use_keepAlive = (ka.toLowerCase()!="false") && (ka.toLowerCase()!=0);
  56. } else {
  57. use_keepAlive = true;
  58. }
  59. connection.onopen = function () {
  60. keepAlive = use_keepAlive;
  61. alert(keepAlive);
  62. webSockKeepAlive();
  63. };
  64. // Log errors
  65. connection.onerror = function (error) {
  66. keepAlive = false;
  67. alert("WebSocket error");
  68. connection.close();
  69. };
  70. // Log messages from the server
  71. connection.onmessage = function (e) {
  72. var lCmd = e.data.substring(0,3);
  73. if (lCmd == "-->") {
  74. console.log(e.data);
  75. var lDirection = Number(e.data.substring(5));
  76. if (e.data[3] == 'h') {
  77. hand_hour.setAttribute("transform", "rotate(" + lDirection + " 800 600)");
  78. }
  79. if (e.data[3] == 'm') {
  80. hand_min.setAttribute("transform", "rotate(" + lDirection + " 800 600)");
  81. }
  82. } else {
  83. websock_text_field.textContent = e.data;
  84. }
  85. };
  86. console.log("load");
  87. }
  88. ]]></script>
  89. <svg class="main"
  90. xmlns="http://www.w3.org/2000/svg"
  91. xmlns:svg="http://www.w3.org/2000/svg"
  92. version="1.1"
  93. xmlns:xlink="http://www.w3.org/1999/xlink"
  94. viewBox="0 0 1600 1200" preserveAspectRatio="xMinYMin meet"
  95. onload="load()"
  96. >
  97. <circle id="line_a" cx="800" cy="600" r="500" style="stroke:rgb(255,0,0); stroke-width:5; fill:rgb(200,200,200)"/>
  98. <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"/>
  99. <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"/>
  100. <text id="websock_text_field" x="800" y="600" text-anchor="middle" font-size="50px" fill="red">No websocket connection yet</text>
  101. </svg>
  102. </body>
  103. </html>