Advertisement
MatthewJ217

WebSocket controller (WIP)

Oct 12th, 2024
9
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.36 KB | None | 0 0
  1. local ws = http.websocket("wss://cc-epoch-interface.glitch.me")
  2. if not ws then error("WebSocket connection failed") end
  3. print("Attempting communication...")
  4. ws.send("turtle new")
  5.  
  6. while true do
  7. local message = ws.receive()
  8. local words = {}
  9. for word in message:gmatch("%w+") do
  10. table.insert(words, word)
  11. end
  12.  
  13. print(message)
  14.  
  15. case = {
  16. move = function()
  17. if words[2] == "w" then
  18. turtle.forward()
  19. elseif words[2] == "a" then
  20. turtle.turnLeft()
  21. elseif words[2] == "s" then
  22. turtle.back()
  23. elseif words[2] == "d" then
  24. turtle.turnRight()
  25. elseif words[2] == "space" or words[2] == "q" then
  26. turtle.up()
  27. elseif words[2] == "shift" or words[2] == "z" then
  28. turtle.down()
  29. elseif words[2] == "i" then
  30. turtle.digDown()
  31. elseif words[2] == "o" then
  32. turtle.dig()
  33. elseif words[2] == "p" then
  34. turtle.digUp()
  35. end
  36. end,
  37.  
  38. default = function()
  39. print("Unrecognized command "..words[1])
  40. end,
  41. }
  42.  
  43. -- execution section
  44. if case[words[1]] then
  45. case[words[1]]()
  46. else
  47. case["default"]()
  48. end
  49. end
  50.  
  51. ws.close()
  52.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement