File: websocket2.lua

package info (click to toggle)
civetweb 1.16%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 5,184 kB
  • sloc: ansic: 32,473; cpp: 1,374; sh: 480; javascript: 204; makefile: 120; php: 11; perl: 6; python: 3
file content (105 lines) | stat: -rw-r--r-- 2,477 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105

function trace(text)
    local f = io.open("websocket2.trace", "a")
    f:write(os.date() .. " - " .. text .. "\n")
    f:close()
end

function iswebsocket()
  return mg.lua_type == "websocket"
end

trace("called with Lua type " .. tostring(mg.lua_type))

if not iswebsocket() then
  trace("no websocket")
  mg.write("HTTP/1.0 403 Forbidden\r\n")
  mg.write("Connection: close\r\n")
  mg.write("\r\n")
  mg.write("forbidden")
  return
end


-- Serialize table to string
function ser(val)
  local t
  if type(val) == "table" then
    for k,v in pairs(val) do
      if t then
        t = t .. ", " .. ser(k) .. "=" .. ser(v)
      else
        t = "{" .. ser(k) .. "=" .. ser(v)
      end
    end
    t = t .. "}"
  else
    t = tostring(val)
  end
  return t
end

-- table of all active connection
allConnections = {}

-- function to get a client identification string
function who(tab)
  local ri = allConnections[tab.client].request_info
  return ri.remote_addr .. ":" .. ri.remote_port
end

-- Callback to accept or reject a connection
function open(tab)
  allConnections[tab.client] = tab
  trace("open[" .. who(tab) .. "]: " .. ser(tab))
  return true -- return true to accept the connection
end

-- Callback for "Websocket ready"
function ready(tab)
  trace("ready[" .. who(tab) .. "]: " .. ser(tab))
  mg.write(tab.client, "text", "Websocket ready")
  mg.write(tab.client, 1, "-->h 180");
  mg.write(tab.client, "-->m 180");
  senddata()
  mg.set_interval(timer, 1)
  return true -- return true to keep the connection open
end

-- Callback for "Websocket received data"
function data(tab)
    trace("data[" .. who(tab) .. "]: " .. ser(tab))
    senddata()
    return true -- return true to keep the connection open
end

-- Callback for "Websocket is closing"
function close(tab)
    trace("close[" .. who(tab) .. "]: " .. ser(tab))
    mg.write("text", "end")
    allConnections[tab.client] = nil
end

function senddata()
    local date = os.date('*t');
    local hand = (date.hour%12)*60+date.min;

    mg.write("text", string.format("%u:%02u:%02u", date.hour, date.min, date.sec));

    if (hand ~= lasthand) then
        mg.write(1, string.format("-->h %f", hand*360/(12*60)));
        mg.write(   string.format("-->m %f", date.min*360/60));
        lasthand = hand;
    end

    if bits and content then
        data(bits, content)
    end
end

function timer()
    trace("timer")
    senddata()
    return true -- return true to keep an interval timer running
end