Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- pastebin run 0YB9PsQV startup={files={mainFrame=<pb:PaSGLHfJ>,WP=<pb:30b8Erjx>},cmds={{'mainFrame'}}} reboot=true label=<input:'Label'>
- os.loadAPI("WP")
- for k, v in pairs(WP) do
- _G[k] = v
- end
- function split(s, delimiter)
- local result = {};
- for match in (s .. delimiter):gmatch("(.-)" .. delimiter) do
- table.insert(result, match);
- end
- return result;
- end
- local CMD_INIT_MAIN_FRAME = "initMainFrame"
- local CMD_WRITE_FILE = "writeFile"
- local CMD_EXECUTE = "execute"
- local CMD_LOG = "log"
- local protocol = ActionProtocol:new()
- local websocket
- function startWebsocket()
- websocket = ProtocolWebsocket:new("wss://nmserver.ddns.net/api", protocol)
- protocol:setOnOpen(function()
- print("requesting MFOS")
- protocol:execute(CMD_INIT_MAIN_FRAME)
- end)
- protocol:setOnAction(function(cmd)
- if cmd[1] == CMD_WRITE_FILE then
- write("downloading file '" .. cmd[2] .. "'... ")
- local answer = http.get("https://nmserver.ddns.net/api/getData?id=" .. cmd[3])
- local f = fs.open(cmd[2], "w")
- f.write(answer.readAll())
- f.close()
- answer.close()
- print("done")
- elseif cmd[1] == CMD_EXECUTE then
- local close = cmd[3] == "true"
- if close then
- write("closing websocket... ")
- websocket:close()
- print("done")
- end
- print("executing '" .. cmd[2] .. "':")
- print()
- shell.run(cmd[2])
- if close then
- rednet.send(os.getComputerID(), { })
- end
- elseif cmd[1] == CMD_LOG then
- print("[server]: " .. cmd[2])
- elseif cmd[1] == "getComputerID" then
- return { "initializer" }
- else
- print("unknown command: " .. cmd[1])
- end
- end)
- protocol:setOnClose(function(code, msg)
- if not code and msg then
- print("onClose: " .. msg)
- return
- end
- if msg then
- print("onClose: " .. code .. ", " .. msg)
- elseif code then
- print("onClose:" .. code)
- end
- end)
- websocket:start()
- end
- print(" ")
- while true do
- local status, err = pcall(startWebsocket)
- if status then
- break
- elseif err == "Terminated" then
- break
- else
- print(err)
- sleep(3)
- end
- end
Add Comment
Please, Sign In to add comment