Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local monitor = peripheral.find("monitor");
- monitor.clear();
- monitor.setCursorPos(1,1);
- local function printM(string)
- term.redirect(monitor);
- print(string);
- term.redirect(term.native());
- end
- local ws;
- local function init(url)
- if ws then
- ws.close();
- ws = nil;
- end
- local connection = http.websocket(url);
- if connection then
- ws = connection;
- print("Connection opened!");
- else
- print("Failed to connect to: " .. url);
- end
- end
- local function cleanup()
- print("Exiting...");
- ws.close();
- ws = nil;
- shell.exit()
- end
- init("ws://localhost:8080");
- while true do
- local event = os.pullEventRaw(); if event == "terminate" then print("Program terminated"); cleanup(); break; end;
- local message = ws.receive();
- if message then
- if message == "close" then
- print("Connection closed.");
- cleanup();
- break;
- end
- printM(message);
- else
- print("Connection closed.");
- cleanup();
- break;
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement