1if 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 15 var connection; 16 var data_field; 17 18 function webSockKeepAlive() { 19 if (keepAlive) { 20 connection.send('Ok'); 21 setTimeout("webSockKeepAlive()", 10000); 22 } 23 } 24 25 function load() { 26 var wsproto = (location.protocol === 'https:') ? "wss:" : "ws:"; 27 connection = new WebSocket(wsproto + "//" + window.location.host + window.location.pathname); 28 data_field = document.getElementById('data'); 29 30 data_field.innerHTML = "wait for data"; 31 32 use_keepAlive = true; 33 34 connection.onopen = function () { 35 keepAlive = use_keepAlive; 36 webSockKeepAlive(); 37 }; 38 39 // Log errors 40 connection.onerror = function (error) { 41 keepAlive = false; 42 alert("WebSocket error"); 43 connection.close(); 44 }; 45 46 // Log messages from the server 47 connection.onmessage = function (e) { 48 data_field.innerHTML = e.data; 49 }; 50 } 51 52</script> 53]====]) 54 55 mg.write("<div id='data'>Wait for page load</div>\r\n") 56 mg.write("</body>\r\n") 57 mg.write("</html>\r\n") 58 return 59end 60 61 62function table.count(tab) 63 local count = 0 64 for _ in pairs(tab) do 65 count = count + 1 66 end 67 return count 68end 69 70 71-- table of all active connection 72allConnections = {} 73connCount = table.count(allConnections) 74 75 76-- function to get a client identification string 77function who(tab) 78 local ri = allConnections[tab.client].request_info 79 return ri.remote_addr .. ":" .. ri.remote_port 80end 81 82-- Callback to accept or reject a connection 83function open(tab) 84 allConnections[tab.client] = tab 85 connCount = table.count(allConnections) 86 return true -- return true to accept the connection 87end 88 89-- Callback for "Websocket ready" 90function ready(tab) 91 senddata() 92 return true -- return true to keep the connection open 93end 94 95-- Callback for "Websocket received data" 96function data(tab) 97 senddata() 98 return true -- return true to keep the connection open 99end 100 101-- Callback for "Websocket is closing" 102function close(tab) 103 allConnections[tab.client] = nil 104 connCount = table.count(allConnections) 105end 106 107function senddata() 108 local date = os.date('*t'); 109 110 collectgarbage("collect"); -- Avoid adding uncollected Lua memory from this state 111 112 mg.write(string.format([[ 113{"Time": "%u:%02u:%02u", 114 "Date": "%04u-%02u-%02u", 115 "Context": %s, 116 "Common": %s, 117 "System": \"%s\", 118 "ws_status": {"Memory": %u, "Connections": %u} 119}]], 120date.hour, date.min, date.sec, 121date.year, date.month, date.day, 122mg.get_info("context"), 123mg.get_info("common"), 124mg.get_info("system"), 125collectgarbage("count")*1024, 126connCount 127)); 128 129end 130 131function timer() 132 senddata() 133 mg.set_timeout("timer()", 1) 134end 135 136mg.set_timeout("timer()", 1) 137 138