ecco7777

CC Web Bridge Slave

Dec 14th, 2025 (edited)
15
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. -- ComputerCraft 1.7 debug polling bot
  2. SERVER = "http://YOUR_ROOT_SERVER_IP:8080"
  3. ID = "minebot1"
  4.  
  5. function poll()
  6. local h = http.get(SERVER .. "/poll?computer_id=" .. ID)
  7. if not h then
  8. print("DEBUG: http.get returned nil")
  9. return nil
  10. end
  11.  
  12. local resp = h.readAll()
  13. print("DEBUG: RAW RESPONSE:", resp)
  14. h.close()
  15.  
  16. local data = nil
  17. local ok, err = pcall(function()
  18. data = textutils.unserialize(resp)
  19. end)
  20. if not ok then
  21. print("DEBUG: Failed to parse response:", err)
  22. return nil
  23. end
  24.  
  25. print("DEBUG: PARSED DATA:", data)
  26. return data
  27. end
  28.  
  29. function sendResult(text)
  30. local payload = textutils.serialize({computer_id = ID, output = text})
  31. print("DEBUG: Sending result:", payload)
  32.  
  33. local ok, err = pcall(function()
  34. http.post(
  35. SERVER .. "/result",
  36. payload,
  37. { ["Content-Type"] = "application/json" }
  38. )
  39. end)
  40. if not ok then
  41. print("DEBUG: Failed to send result:", err)
  42. end
  43. end
  44.  
  45. while true do
  46. data = poll()
  47. if data ~= nil and data.command ~= nil then
  48. print("DEBUG: COMMAND RECEIVED:", data.command)
  49. local ok, err = pcall(function()
  50. shell.run(data.command)
  51. end)
  52. if ok then
  53. sendResult("OK: " .. data.command)
  54. else
  55. sendResult("ERROR: " .. tostring(err))
  56. end
  57. else
  58. print("DEBUG: No command received")
  59. end
  60. sleep(1)
  61. end
  62.  
Advertisement
Add Comment
Please, Sign In to add comment