Skip to content

Instantly share code, notes, and snippets.

@Castux
Last active October 29, 2020 08:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Castux/9b585e58d3505664907b699d02a1a16f to your computer and use it in GitHub Desktop.
Save Castux/9b585e58d3505664907b699d02a1a16f to your computer and use it in GitHub Desktop.
local sockets = {}
local weblit = require "weblit"
local app = weblit.app
app.bind {host = "0.0.0.0", port = 8080 }
app.use(weblit.logger)
app.use(weblit.autoHeaders)
app.route({ path = "/" }, weblit.static("client"))
local function socketHandler (req, read, write)
print("New client")
sockets[req.socket] = function(str)
write({
opcode = 1,
payload = str
})
end
for message in read do
print("Got:", message.payload)
for sock,writer in pairs(sockets) do
if sock ~= req.socket then
writer(message.payload .. " back")
end
end
end
write()
print("Client left")
sockets[req.socket] = nil
end
app.websocket({ path = "/ws" }, socketHandler)
app.start()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment