ws_status.lua 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. if mg.lua_type ~= "websocket" then
  2. mg.write("HTTP/1.0 200 OK\r\n")
  3. mg.write("Connection: close\r\n")
  4. mg.write("\r\n")
  5. mg.write("<!DOCTYPE HTML>\r\n")
  6. mg.write("<html xmlns=\"http://www.w3.org/1999/xhtml\">\r\n")
  7. mg.write("<head>\r\n")
  8. mg.write("<meta charset=\"UTF-8\"></meta>\r\n")
  9. mg.write("<title>Server stats</title>\r\n")
  10. mg.write("</head>\r\n")
  11. mg.write("<body onload=\"load()\">\r\n")
  12. mg.write([====[
  13. <script type="text/javascript">
  14. var connection;
  15. var data_field;
  16. function webSockKeepAlive() {
  17. if (keepAlive) {
  18. connection.send('Ok');
  19. setTimeout("webSockKeepAlive()", 10000);
  20. }
  21. }
  22. function load() {
  23. var wsproto = (location.protocol === 'https:') ? "wss:" : "ws:";
  24. connection = new WebSocket(wsproto + "//" + window.location.host + window.location.pathname);
  25. data_field = document.getElementById('data');
  26. data_field.innerHTML = "wait for data";
  27. use_keepAlive = true;
  28. connection.onopen = function () {
  29. keepAlive = use_keepAlive;
  30. webSockKeepAlive();
  31. };
  32. // Log errors
  33. connection.onerror = function (error) {
  34. keepAlive = false;
  35. alert("WebSocket error");
  36. connection.close();
  37. };
  38. // Log messages from the server
  39. connection.onmessage = function (e) {
  40. data_field.innerHTML = e.data;
  41. };
  42. }
  43. </script>
  44. ]====])
  45. mg.write("<div id='data'>Wait for page load</div>\r\n")
  46. mg.write("</body>\r\n")
  47. mg.write("</html>\r\n")
  48. return
  49. end
  50. -- table of all active connection
  51. allConnections = {}
  52. -- function to get a client identification string
  53. function who(tab)
  54. local ri = allConnections[tab.client].request_info
  55. return ri.remote_addr .. ":" .. ri.remote_port
  56. end
  57. -- Callback to accept or reject a connection
  58. function open(tab)
  59. allConnections[tab.client] = tab
  60. return true -- return true to accept the connection
  61. end
  62. -- Callback for "Websocket ready"
  63. function ready(tab)
  64. senddata()
  65. return true -- return true to keep the connection open
  66. end
  67. -- Callback for "Websocket received data"
  68. function data(tab)
  69. senddata()
  70. return true -- return true to keep the connection open
  71. end
  72. -- Callback for "Websocket is closing"
  73. function close(tab)
  74. allConnections[tab.client] = nil
  75. end
  76. function senddata()
  77. local date = os.date('*t');
  78. mg.write(string.format([[
  79. {"Time": "%u:%02u:%02u", "Context": %s, "Common": %s, "System": \"%s\"}]],
  80. date.hour, date.min, date.sec,
  81. mg.get_info("context"),
  82. mg.get_info("common"),
  83. mg.get_info("system")
  84. ));
  85. end
  86. function timer()
  87. senddata()
  88. mg.set_timeout("timer()", 1)
  89. end
  90. mg.set_timeout("timer()", 1)