ws_status.lua 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. function table.count(tab)
  51. local count = 0
  52. for _ in pairs(tab) do
  53. count = count + 1
  54. end
  55. return count
  56. end
  57. -- table of all active connection
  58. allConnections = {}
  59. connCount = table.count(allConnections)
  60. -- function to get a client identification string
  61. function who(tab)
  62. local ri = allConnections[tab.client].request_info
  63. return ri.remote_addr .. ":" .. ri.remote_port
  64. end
  65. -- Callback to accept or reject a connection
  66. function open(tab)
  67. allConnections[tab.client] = tab
  68. connCount = table.count(allConnections)
  69. return true -- return true to accept the connection
  70. end
  71. -- Callback for "Websocket ready"
  72. function ready(tab)
  73. senddata()
  74. return true -- return true to keep the connection open
  75. end
  76. -- Callback for "Websocket received data"
  77. function data(tab)
  78. senddata()
  79. return true -- return true to keep the connection open
  80. end
  81. -- Callback for "Websocket is closing"
  82. function close(tab)
  83. allConnections[tab.client] = nil
  84. connCount = table.count(allConnections)
  85. end
  86. function senddata()
  87. local date = os.date('*t');
  88. collectgarbage("collect"); -- Avoid adding uncollected Lua memory from this state
  89. mg.write(string.format([[
  90. {"Time": "%u:%02u:%02u",
  91. "Date": "%04u-%02u-%02u",
  92. "Context": %s,
  93. "Common": %s,
  94. "System": \"%s\",
  95. "ws_status": {"Memory": %u, "Connections": %u}
  96. }]],
  97. date.hour, date.min, date.sec,
  98. date.year, date.month, date.day,
  99. mg.get_info("context"),
  100. mg.get_info("common"),
  101. mg.get_info("system"),
  102. collectgarbage("count")*1024,
  103. connCount
  104. ));
  105. end
  106. function timer()
  107. senddata()
  108. mg.set_timeout("timer()", 1)
  109. end
  110. mg.set_timeout("timer()", 1)