5 local f = io.open("websocket.trace", "a")
6 f:write(os.date() .. " - " .. text .. "\n")
10 function iswebsocket()
11 return mg.lua_type == "websocket"
12 --return pcall(function()
13 -- if (string.upper(mg.request_info.http_headers.Upgrade)~="WEBSOCKET") then error("") end
17 trace("called with Lua type " .. tostring(mg.lua_type))
19 if not iswebsocket() then
21 mg.write("HTTP/1.0 403 Forbidden\r\n")
22 mg.write("Connection: close\r\n")
29 -- Serialize table to string
32 if type(val) == "table" then
33 for k,v in pairs(val) do
35 t = t .. ", " .. ser(k) .. "=" .. ser(v)
37 t = "{" .. ser(k) .. "=" .. ser(v)
47 -- table of all active connection
50 -- function to get a client identification string
52 local ri = allConnections[tab.client].request_info
53 return ri.remote_addr .. ":" .. ri.remote_port
56 -- Callback to accept or reject a connection
58 allConnections[tab.client] = tab
59 trace("open[" .. who(tab) .. "]: " .. ser(tab))
60 return true -- return true to accept the connection
63 -- Callback for "Websocket ready"
65 trace("ready[" .. who(tab) .. "]: " .. ser(tab))
66 mg.write(tab.client, "text", "Websocket ready")
67 mg.write(tab.client, 1, "-->h 180");
68 mg.write(tab.client, "-->m 180");
70 if timerID == "timeout" then
71 mg.set_timeout("timer()", 1)
72 elseif timerID == "interval" then
73 mg.set_interval("timer()", 1)
75 return true -- return true to keep the connection open
78 -- Callback for "Websocket received data"
80 trace("data[" .. who(tab) .. "]: " .. ser(tab))
82 return true -- return true to keep the connection open
85 -- Callback for "Websocket is closing"
87 trace("close[" .. who(tab) .. "]: " .. ser(tab))
88 mg.write("text", "end")
89 allConnections[tab.client] = nil
93 local date = os.date('*t');
94 local hand = (date.hour%12)*60+date.min;
96 mg.write("text", string.format("%u:%02u:%02u", date.hour, date.min, date.sec));
98 if (hand ~= lasthand) then
99 mg.write(1, string.format("-->h %u", hand*360/(12*60)));
100 mg.write( string.format("-->m %u", date.min*360/60));
104 if bits and content then
112 if timerID == "timeout" then
113 mg.set_timeout("timer()", 1)
115 return true -- return true to keep an interval timer running