and_cesbo icon

Astra. WebSocket auth_request

and_cesbo | PRO | 10/17/16 07:19:52 AM UTC | 0 ⭐ | 534 👁️ | Never ⏰ | []
Lua |

970 B

|

None

|

0 👍

/

0 👎

ws_list = {}
ws_active = false
 
ws = http_request({
    host = "127.0.0.1",
    port = 8000,
    path = "/",
    headers = {
        "Origin: http://127.0.0.1:8000",
        "Upgrade: websocket",
        "Connection: Upgrade",
    },
    callback = function(request, response)
        if not response then
            ws_active = false
            return nil
        elseif type(response) == "table" then
            ws_active = true
            return nil
        end
 
        local x = json.decode(response)
        local callback = ws_list[x.client_id]
        if callback then
            callback(x.code == "ok")
        end
    end
})
 
function auth_request(client_id, request, callback)
    if not request then
        ws_list[client_id] = nil
        return nil
    end
 
    ws_list[client_id] = callback
    if ws_active then
        ws:send(json.encode({ client_id = client_id, request = request }))
    else
        callback(true)
    end
end

Comments