echo.lua 928 B

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