1 2if mg.lua_type ~= "websocket" then 3 mg.write("HTTP/1.0 403 Forbidden\r\n") 4 mg.write("Connection: close\r\n") 5 mg.write("\r\n") 6 mg.write("forbidden") 7 return 8end 9 10 11-- table of all active connection 12allConnections = {} 13 14-- function to get a client identification string 15function who(tab) 16 local ri = allConnections[tab.client].request_info 17 return ri.remote_addr .. ":" .. ri.remote_port 18end 19 20-- Callback to accept or reject a connection 21function open(tab) 22 allConnections[tab.client] = tab 23 return true -- return true to accept the connection 24end 25 26-- Callback for "Websocket ready" 27function ready(tab) 28 return true -- return true to keep the connection open 29end 30 31-- Callback for "Websocket received data" 32function data(tab) 33 mg.write(1, tab.data); 34 return true -- return true to keep the connection open 35end 36 37-- Callback for "Websocket is closing" 38function close(tab) 39 allConnections[tab.client] = nil 40end 41 42