websocket.lua 2.8 KB

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