1
0
mirror of https://github.com/lammertb/libhttp.git synced 2025-08-16 11:21:57 +03:00

Rewrite websocket for Lua (Step 9 of ?)

This commit is contained in:
bel
2014-05-24 00:14:37 +02:00
parent 6e8415d4e5
commit ab3bc563cd
2 changed files with 45 additions and 27 deletions

View File

@@ -41,16 +41,25 @@ function ser(val)
return t
end
-- table of all active connection
allConnections = {}
-- function to get a client identification string
function who(tab)
local ri = allConnections[tab.client]
return ri.remote_addr .. ":" .. ri.remote_port
end
-- Callback to reject a connection
function open()
trace("open")
function open(tab)
allConnections[tab.client] = tab.request_info
trace("open[" .. who(tab) .. "]: " .. ser(tab))
return true
end
-- Callback for "Websocket ready"
function ready(tab)
trace("ready: " .. ser(tab))
trace("ready[" .. who(tab) .. "]: " .. ser(tab))
mg.write("text", "Websocket ready")
senddata()
return true
@@ -58,15 +67,16 @@ end
-- Callback for "Websocket received data"
function data(tab)
trace("data: " .. ser(tab))
trace("data[" .. who(tab) .. "]: " .. ser(tab))
senddata()
return true
end
-- Callback for "Websocket is closing"
function close(tab)
trace("close: " .. ser(tab))
trace("close[" .. who(tab) .. "]: " .. ser(tab))
mg.write("text", "end")
allConnections[tab.client] = nil
end
function senddata()