1timerID = "timeout"
2--timerID = "interval"
3
4function trace(text)
5    local f = io.open("websocket.trace", "a")
6    f:write(os.date() .. " - " .. text .. "\n")
7    f:close()
8end
9
10function 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
14  --end)
15end
16
17trace("called with Lua type " .. tostring(mg.lua_type))
18
19if not iswebsocket() then
20  trace("no websocket")
21  mg.write("HTTP/1.0 403 Forbidden\r\n")
22  mg.write("Connection: close\r\n")
23  mg.write("\r\n")
24  mg.write("forbidden")
25  return
26end
27
28
29-- Serialize table to string
30function ser(val)
31  local t
32  if type(val) == "table" then
33    for k,v in pairs(val) do
34      if t then
35        t = t .. ", " .. ser(k) .. "=" .. ser(v)
36      else
37        t = "{" .. ser(k) .. "=" .. ser(v)
38      end
39    end
40    t = t .. "}"
41  else
42    t = tostring(val)
43  end
44  return t
45end
46
47-- table of all active connection
48allConnections = {}
49
50-- function to get a client identification string
51function who(tab)
52  local ri = allConnections[tab.client].request_info
53  return ri.remote_addr .. ":" .. ri.remote_port
54end
55
56-- Callback to accept or reject a connection
57function open(tab)
58  allConnections[tab.client] = tab
59  trace("open[" .. who(tab) .. "]: " .. ser(tab))
60  return true -- return true to accept the connection
61end
62
63-- Callback for "Websocket ready"
64function ready(tab)
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");
69  senddata()
70  if timerID == "timeout" then
71    mg.set_timeout("timer()", 1)
72  elseif timerID == "interval" then
73    mg.set_interval("timer()", 1)
74  end
75  return true -- return true to keep the connection open
76end
77
78-- Callback for "Websocket received data"
79function data(tab)
80    trace("data[" .. who(tab) .. "]: " .. ser(tab))
81    senddata()
82    return true -- return true to keep the connection open
83end
84
85-- Callback for "Websocket is closing"
86function close(tab)
87    trace("close[" .. who(tab) .. "]: " .. ser(tab))
88    mg.write("text", "end")
89    allConnections[tab.client] = nil
90end
91
92function senddata()
93    local date = os.date('*t');
94    local hand = (date.hour%12)*60+date.min;
95
96    mg.write("text", string.format("%u:%02u:%02u", date.hour, date.min, date.sec));
97
98    if (hand ~= lasthand) then
99        mg.write(1, string.format("-->h %f", hand*360/(12*60)));
100        mg.write(   string.format("-->m %f", date.min*360/60));
101        lasthand = hand;
102    end
103
104    if bits and content then
105        data(bits, content)
106    end
107end
108
109function timer()
110    trace("timer")
111    senddata()
112    if timerID == "timeout" then
113        mg.set_timeout("timer()", 1)
114    else
115        return true -- return true to keep an interval timer running
116    end
117end
118
119