Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local ws = require("websocket_client");
- local event = require("event");
- local computer = require("computer");
- local os = require("os");
- local io = require("io");
- local cl;
- local ip, port = "192.168.0.69", 8080;
- local function save(message)
- print("Saving File.");
- local fileName, content = message:match("(%S+) (.+)");
- local file = io.open(fileName, "w");
- file:write(content);
- file:close();
- end
- local function sync(message)
- print("Syncing File.");
- local filename = message;
- local file = io.open(filename, "r");
- local content = "";
- while true do
- if (not file) then break end
- local buffer = file:read(128);
- if (not buffer) then break end
- content = content .. buffer;
- end
- cl:send(content)
- end
- local ops = {save=save, sync=sync}
- local function onMessage(e, msg)
- if (e == "close_request") then os.exit() end
- local op, msg = msg:match("(%S+) (.+)");
- ops[op](msg);
- end
- cl = ws.create(onMessage, true);
- cl:connect(ip, port, "/", false);
- os.sleep(0.5);
- cl:send(computer.address());
- while true do
- local e = {event.pull()};
- if (e[1] == "interrupted") then break end
- end
- cl:disconnect();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement