melzneni

CC_Websocket

Oct 21st, 2020 (edited)
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.79 KB | None | 0 0
  1. local JSON=require "libraries/json";
  2.  
  3. local args={...}
  4.  
  5. if (#args<1) then error("please provide a token") end
  6.  
  7. local token=args[1]
  8.  
  9. local dataDir="nms"
  10.  
  11. local id=nil
  12.  
  13. if (fs.exists(dataDir.."/nmsId")) then
  14.     local f=fs.open(dataDir.."/nmsId")
  15.     id=tonumber(f.read())
  16.     f.close()
  17. end
  18.  
  19. local ws, err = http.websocket("wss://nmserver.ddns.net/websocket/")
  20. if ws then
  21.     print("performing login...")
  22.     if (id==nil) then
  23.         ws.send("{\"login\":\""..token.."\"}")
  24.     else
  25.         ws.send("{\"login\":\""..token.."\",id:"..id+"}")
  26.     end
  27.     local answer = JSON.parse(ws.receive());
  28.     print(answer)
  29.     if (answer["status"]~="accepted")then
  30.         print("token rejected")
  31.         return
  32.     end
  33.     print("token accepted: "..answer["id"])
  34.     ws.close()
  35. else print(err)
  36. end
Add Comment
Please, Sign In to add comment