Advertisement
gravityio

Untitled

Nov 7th, 2021
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.98 KB | None | 0 0
  1. local ip, port = "192.168.0.69", 8080;
  2.  
  3. local id = os.getComputerID();
  4.  
  5. local ws = nil;
  6.  
  7. local function onSave(message)
  8.     local fileName, content = message:match("(%S+) (.+)");
  9.     local file = fs.open(fileName, "w");
  10.     if (file) then file.write(content); end
  11. end
  12.  
  13. local function onSync(message)
  14.     local fileName = message;
  15.     local file = fs.open(fileName, "r");
  16.     local content = "";
  17.     if (file) then content = file.readAll(); end
  18.     ws.send(content);
  19. end
  20.  
  21. local ops = {sync=onSync, save=onSave};
  22.  
  23. local function onMessage(message)
  24.     local op, msg = message:match("(%S+) (.+)");    
  25.     ops[op](msg);
  26. end
  27.  
  28. while true do
  29.     ws = http.websocket("ws://"..ip..":"..port);
  30.     if (ws) then
  31.         print("Connected");
  32.         sleep(0.5);
  33.         ws.send(id);
  34.         while true do
  35.             if (ws) then
  36.             local message = ws.receive();
  37.             onMessage(message);
  38.             else break end
  39.         end
  40.     else sleep(1); end
  41. end
  42.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement