and_cesbo

Astra. WebSocket auth_request

Oct 17th, 2016
440
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.97 KB | None | 0 0
  1. ws_list = {}
  2. ws_active = false
  3.  
  4. ws = http_request({
  5.     host = "127.0.0.1",
  6.     port = 8000,
  7.     path = "/",
  8.     headers = {
  9.         "Origin: http://127.0.0.1:8000",
  10.         "Upgrade: websocket",
  11.         "Connection: Upgrade",
  12.     },
  13.     callback = function(request, response)
  14.         if not response then
  15.             ws_active = false
  16.             return nil
  17.         elseif type(response) == "table" then
  18.             ws_active = true
  19.             return nil
  20.         end
  21.  
  22.         local x = json.decode(response)
  23.         local callback = ws_list[x.client_id]
  24.         if callback then
  25.             callback(x.code == "ok")
  26.         end
  27.     end
  28. })
  29.  
  30. function auth_request(client_id, request, callback)
  31.     if not request then
  32.         ws_list[client_id] = nil
  33.         return nil
  34.     end
  35.  
  36.     ws_list[client_id] = callback
  37.     if ws_active then
  38.         ws:send(json.encode({ client_id = client_id, request = request }))
  39.     else
  40.         callback(true)
  41.     end
  42. end
Advertisement
Add Comment
Please, Sign In to add comment