william200027

WebSocket

Jun 7th, 2021 (edited)
673
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local modem = peripheral.wrap('right')
  2.  
  3. local connectionURL = "ws://51.83.42.69:9898/"
  4. local ws, err = http.websocket(connectionURL)
  5.  
  6. cube = {}
  7.  
  8. if not ws then
  9.   return printError(err)
  10. end
  11.  
  12. ws.send(textutils.serializeJSON({type='MESSAGE',data={'ComputerCraft Connected !'}}))
  13.  
  14. local _, url, response, isBinary = os.pullEvent("websocket_message")
  15. if url == connectionURL then
  16.   print(response)
  17. end
  18.  
  19. function name()
  20.   data = modem.getNamesRemote()
  21. end
  22.  
  23. function check()
  24.   number = 0
  25.   for i=0,500 do
  26.     if data[i] == nil then
  27.     else
  28.       cube[i] = peripheral.wrap(data[i])
  29.       number = number + 1
  30.    end
  31.   end
  32. end
  33.  
  34. function get()
  35.   Energy = {}
  36.   MEnergy = {}
  37.   for i=1,number do
  38.     Energy[i] = cube[i].getEnergy()
  39.     MEnergy[i] = cube[i].getEnergyCapacity()
  40.   end
  41. end
  42.  
  43. function send()
  44.   ws.send(textutils.serializeJSON({type='INT',data={name=data,energy=Energy,menergy=MEnergy}}))
  45. end
  46.  
  47. while true do
  48.   name()
  49.   check()
  50.   get()
  51.   send()
  52. sleep(0.1)
  53. end
RAW Paste Data