Skip to content

Instantly share code, notes, and snippets.

@Castux
Last active October 29, 2020 08:10
Show Gist options
  • Save Castux/9b585e58d3505664907b699d02a1a16f to your computer and use it in GitHub Desktop.
Save Castux/9b585e58d3505664907b699d02a1a16f to your computer and use it in GitHub Desktop.

Revisions

  1. Castux revised this gist Oct 27, 2020. 1 changed file with 5 additions and 8 deletions.
    13 changes: 5 additions & 8 deletions wstest.lua
    Original file line number Diff line number Diff line change
    @@ -1,17 +1,14 @@
    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')
    local weblit = require "weblit"
    local app = weblit.app

    app.bind {host = "0.0.0.0", port = 8080 }

    app.use(logger)
    app.use(auto_headers)
    app.use(weblit.logger)
    app.use(weblit.autoHeaders)

    app.route({ path = "/" }, static("client"))
    app.route({ path = "/" }, weblit.static("client"))

    local function socketHandler (req, read, write)
    print("New client")
  2. Castux created this gist Oct 27, 2020.
    42 changes: 42 additions & 0 deletions wstest.lua
    Original 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()