Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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
- local function executeInstruction(s)
- if s == "::close" then
- print("Connection closed.");
- cleanup();
- return 0;
- elseif s == "::forward" then
- forward();
- return 0;
- elseif s == "::back" then
- back();
- return 0;
- elseif s == "::left" then
- left();
- return 0;
- elseif s == "::right" then
- right();
- return 0;
- elseif s == "::up" then
- up();
- return 0;
- elseif s == "::down" then
- down();
- return 0;
- end
- return -1;
- 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 executeInstruction(message) == -1 then
- print(message);
- end
- else
- print("Connection closed.");
- cleanup();
- break;
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement