Advertisement
Snowdingerr

CC Tweaked: BeeCode

May 9th, 2021 (edited)
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.12 KB | Source Code | 0 0
  1. -- startup.lua
  2.  
  3. function websocketLoop()
  4.     local ws, err = http.websocket("ws://localhost:8080/turtle")
  5.  
  6.     if err then
  7.         print(err)
  8.     elseif ws then
  9.         ws.send("turtleId=" .. os.getComputerID())
  10.         while true do
  11.             term.clear()
  12.             term.setCursorPos(1, 1)
  13.             print("== Bee#" .. os.getComputerID() .. " ==")
  14.             print("fuel: " .. turtle.getFuelLevel())
  15.             ws.send("next")
  16.             local message = ws.receive()
  17.             if message == nil then
  18.                 break
  19.             end
  20.             local command = load(message)
  21.             local status, result = pcall(command)
  22.             ws.send(textutils.serialize({status = status, result = result}))
  23.             os.sleep(2)
  24.         end
  25.     end
  26.     if ws then
  27.         ws.close()
  28.     end
  29. end
  30.  
  31. while true do
  32.     local status, res = pcall(websocketLoop)
  33.     term.clear()
  34.     term.setCursorPos(1, 1)
  35.     if res == 'Terminated' then
  36.         print("Shutting down...")
  37.         os.sleep(1)
  38.         print("done.")
  39.         break
  40.     end
  41.     print("-- no connection to hive --")
  42.     os.sleep(5)
  43. end
  44.  
Tags: lua cc:tweaked
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement