Last active
October 29, 2020 08:10
-
-
Save Castux/9b585e58d3505664907b699d02a1a16f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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