CaptainResu

HiveMind turtle

Feb 22nd, 2021 (edited)
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.98 KB | None | 0 0
  1. if http then
  2.     print("HTTP is enabled!")
  3. else
  4.     print("HTTP is disabled! Stopping!")
  5.     return -1
  6. end
  7.  
  8. local openUrl = "ws://192.168.0.126"
  9.  
  10. msgCheck = nil
  11. running = false
  12. local ws, err
  13.  
  14. function moveDataAccept (action)
  15.     print(action)
  16.     if action then
  17.         local fullAction = "return("..action..")"
  18.         local fun = assert(loadstring(fullAction))
  19.         local res = fun()
  20.         print(res)
  21.         ws.send(res)
  22.     end
  23. end
  24.  
  25. while true do
  26.     -- ws, err = http.websocket(openUrl)
  27.     repeat
  28.         ws, err = http.websocket(openUrl)
  29.         if ws then
  30.             print("Websocket opened!")
  31.             running = true
  32.  
  33.             ws.send('{"myType": "mining_turtle"}')
  34.             while running do
  35.                 if pcall(function ()
  36.                     local msg = ws.receive()
  37.                     moveDataAccept(msg)            
  38.                 end) then
  39.                     -- print("Success")
  40.                 else
  41.                     print("Error!")
  42.                     running = false
  43.                 end
  44.             end
  45.             -- running = false
  46.             print("Stopped!")
  47.             pcall(function()
  48.                 ws.close()
  49.             end)
  50.         else
  51.             print(err)
  52.             sleep(5)
  53.         end
  54.     until not true
  55. end
Add Comment
Please, Sign In to add comment