Advertisement
Dragoon209

WebListen

Dec 26th, 2023
970
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.29 KB | Source Code | 0 0
  1.  
  2.  
  3.  
  4. local myURL = "wss://team-david-gaither-david-gaither-1e5fac37.flowfuse.cloud/public/websocks"
  5. local ws = http.websocket(myURL)
  6.  
  7. local comptuerID = os.getComputerID
  8.  
  9. local data = {}
  10.  
  11. ws.send("Program Start-")
  12.  
  13.  
  14. function reconnect()
  15.     ws = http.websocket(myURL)
  16.     repeat
  17.         data = {os.pullEvent()}
  18.         until data[1] == "websocket_failure" or data[1] == "websocket_message"
  19.     end
  20.  
  21.     if data[1] == "websocket_failure" then
  22.         print("connection failed, waiting 30 seconds")
  23.         sleep(30)
  24.         return false
  25.     elseif data[1] == "websocket_message" then
  26.         print("reconnected")
  27.         return data[3]
  28. end
  29.  
  30.  
  31. function sendMsg(msg)
  32.     ws.send(msg)
  33. end
  34.  
  35.  
  36. function waitMsg()
  37.     local timer_id = os.startTimer(30)
  38.     print("timer id: " .. timer_id)
  39.  
  40.     repeat
  41.         waitdata = {os.pullEvent()}
  42.     until waitdata[1] ~= nil
  43.  
  44.     --print("Received message from " .. tostring(waitdata[1]) .. " with contents " .. tostring(waitdata[2]) .. " with event " .. tostring(waitdata[3]))
  45.  
  46.     if waitdata[1] == "timer" then ws.send("heartbeat") end --and waitdata[2] == timer_id
  47.  
  48.     if waitdata[1] == "websocket_failure" then reconnect() end
  49.  
  50.     if waitdata[1] == "websocket_message" then
  51.         if string.find(tostring(waitdata[2]), "pushScores") then
  52.             rednet.send(1, "pushScores")
  53.         end
  54.     end
  55. end
  56.  
  57. while true do waitMsg() end
  58.  
  59.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement