Advertisement
gravityio

Untitled

Nov 7th, 2021
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. local ws = require("websocket_client");
  2. local event = require("event");
  3. local computer = require("computer");
  4. local os = require("os");
  5. local io = require("io");
  6. local cl;
  7.  
  8. local ip, port = "192.168.0.69", 8080;
  9.  
  10. local function save(message)
  11. print("Saving File.");
  12. local fileName, content = message:match("(%S+) (.+)");
  13. local file = io.open(fileName, "w");
  14. file:write(content);
  15. file:close();
  16. end
  17.  
  18. local function sync(message)
  19. print("Syncing File.");
  20. local filename = message;
  21. local file = io.open(filename, "r");
  22. local content = "";
  23. while true do
  24. if (not file) then break end
  25. local buffer = file:read(128);
  26. if (not buffer) then break end
  27. content = content .. buffer;
  28. end
  29. cl:send(content)
  30. end
  31.  
  32. local ops = {save=save, sync=sync}
  33. local function onMessage(e, msg)
  34. if (e == "close_request") then os.exit() end
  35. local op, msg = msg:match("(%S+) (.+)");
  36. ops[op](msg);
  37. end
  38.  
  39. cl = ws.create(onMessage, true);
  40.  
  41. cl:connect(ip, port, "/", false);
  42. os.sleep(0.5);
  43. cl:send(computer.address());
  44.  
  45. while true do
  46. local e = {event.pull()};
  47. if (e[1] == "interrupted") then break end
  48. end
  49.  
  50. cl:disconnect();
  51.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement