-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconturtle.lua
More file actions
67 lines (54 loc) · 1.7 KB
/
conturtle.lua
File metadata and controls
67 lines (54 loc) · 1.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
-- vvv CHANGE THIS vvv --
local URL = "WEBSOCKETURL"
local PORT = "PORT"
-- ^^^^^^^^^^^^^^^^^^^ --
local wsAddr = "ws://" .. URL .. ":" .. PORT
print(wsAddr)
if wsAddr == "ws://WEBSOCKETURL:PORT" then
print("PLEASE SET THE URL BEFORE RUNNING")
return
end
-- Open a WebSocket connection
local ws, connectError = http.websocket(wsAddr)
if not ws then
print("Error connecting to WebSocket: " .. connectError)
return
end
local function fromJSON(json)
return textutils.unserialiseJSON(json)
end
local function toJSON(table)
return textutils.serialiseJSON(table)
end
local function main()
print("Connected to WebSocket server.")
ws.send("renameSelf|" .. os.getComputerLabel())
while true do
local msg, err = ws.receive()
msg = fromJSON(msg)
if msg then
print(msg)
if msg["code"] then
print("Received code: " .. msg["code"])
local func, loadErr = load(msg["code"])
if func then
local success, runErr = pcall(func)
if not success then
print("Error running code: " .. tostring(runErr))
end
else
print("Error loading code: " .. tostring(loadErr))
end
else
print("No script found in the message")
end
else
print("Error receiving message: " .. tostring(err))
end
end
end
local ok, errorMessage = pcall(main)
pcall(ws and ws.close or function()end)
if not ok then
printError(errorMessage) -- print the error like it normally would
end