websocket.xhtml 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. function webSockKeepAlive() {
  19. if (keepAlive) {
  20. connection.send('client still alive');
  21. setTimeout("webSockKeepAlive()", 10000);
  22. }
  23. }
  24. function load() {
  25. connection = new WebSocket("ws://" + window.location.host + "/websocket.lua");
  26. websock_text_field = document.getElementById('websock_text_field');
  27. connection.onopen = function () {
  28. keepAlive = true;
  29. webSockKeepAlive();
  30. };
  31. // Log errors
  32. connection.onerror = function (error) {
  33. keepAlive = false;
  34. alert("WebSocket error");
  35. connection.close();
  36. };
  37. // Log messages from the server
  38. connection.onmessage = function (e) {
  39. websock_text_field.textContent = e.data;
  40. };
  41. }
  42. ]]></script>
  43. <svg class="main"
  44. xmlns="http://www.w3.org/2000/svg"
  45. xmlns:svg="http://www.w3.org/2000/svg"
  46. version="1.1"
  47. xmlns:xlink="http://www.w3.org/1999/xlink"
  48. viewBox="0 0 1600 1200" preserveAspectRatio="xMinYMin meet"
  49. onload="load()"
  50. >
  51. <circle id="line_a" cx="800" cy="600" r="500" style="stroke:rgb(255,0,0); stroke-width:5; fill:rgb(200,200,200)"/>
  52. <text id="websock_text_field" x="800" y="600" text-anchor="middle" font-size="50px" fill="red">No websocket connection yet</text>
  53. </svg>
  54. </body>
  55. </html>