summaryrefslogtreecommitdiffstats
path: root/src/civetweb/test/websocket.lua
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 18:45:59 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 18:45:59 +0000
commit19fcec84d8d7d21e796c7624e521b60d28ee21ed (patch)
tree42d26aa27d1e3f7c0b8bd3fd14e7d7082f5008dc /src/civetweb/test/websocket.lua
parentInitial commit. (diff)
downloadceph-6d07fdb6bb33b1af39833b850bb6cf8af79fe293.tar.xz
ceph-6d07fdb6bb33b1af39833b850bb6cf8af79fe293.zip
Adding upstream version 16.2.11+ds.upstream/16.2.11+dsupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/civetweb/test/websocket.lua')
-rw-r--r--src/civetweb/test/websocket.lua118
1 files changed, 118 insertions, 0 deletions
diff --git a/src/civetweb/test/websocket.lua b/src/civetweb/test/websocket.lua
new file mode 100644
index 000000000..7338ed874
--- /dev/null
+++ b/src/civetweb/test/websocket.lua
@@ -0,0 +1,118 @@
+timerID = "timeout"
+--timerID = "interval"
+
+function trace(text)
+ local f = io.open("websocket.trace", "a")
+ f:write(os.date() .. " - " .. text .. "\n")
+ f:close()
+end
+
+function iswebsocket()
+ return mg.lua_type == "websocket"
+ --return pcall(function()
+ -- if (string.upper(mg.request_info.http_headers.Upgrade)~="WEBSOCKET") then error("") end
+ --end)
+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()
+ if timerID == "timeout" then
+ mg.set_timeout("timer()", 1)
+ elseif timerID == "interval" then
+ mg.set_interval("timer()", 1)
+ end
+ 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 %u", hand*360/(12*60)));
+ mg.write( string.format("-->m %u", date.min*360/60));
+ lasthand = hand;
+ end
+
+ if bits and content then
+ data(bits, content)
+ end
+end
+
+function timer()
+ trace("timer")
+ senddata()
+ if timerID == "timeout" then
+ mg.set_timeout("timer()", 1)
+ else
+ return true -- return true to keep an interval timer running
+ end
+end
+