Last active
October 29, 2020 08:10
-
-
Save Castux/9b585e58d3505664907b699d02a1a16f to your computer and use it in GitHub Desktop.
Revisions
-
Castux revised this gist
Oct 27, 2020 . 1 changed file with 5 additions and 8 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,17 +1,14 @@ 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") -
Castux created this gist
Oct 27, 2020 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,42 @@ local sockets = {} local ws = require('weblit-websocket') local app = require('weblit-app') local static = require('weblit-static') local logger = require('weblit-logger') local auto_headers = require('weblit-auto-headers') app.bind {host = "0.0.0.0", port = 8080 } app.use(logger) app.use(auto_headers) app.route({ path = "/" }, 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()