Advertisement
gravityio

Code.lua

Nov 7th, 2021
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.06 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.     file.write(content);
  11.     file.close();
  12.     print("Writing file, " .. fileName);
  13. end
  14.  
  15. local function onSync(message)
  16.     local fileName = message;
  17.     local file = fs.open(fileName, "r");
  18.     local content = "";
  19.     if (file) then content = file.readAll(); end
  20.     ws.send(content);
  21.     file.close();
  22.     print("Syncing file, " .. fileName);
  23. end
  24.  
  25. local ops = {sync=onSync, save=onSave};
  26.  
  27. local function onMessage(message)
  28.     local op, msg = message:match("(%S+) (.+)");    
  29.     ops[op](msg);
  30. end
  31.  
  32. while true do
  33.   ws = http.websocket("ws://"..ip..":"..port);
  34.   if (ws) then
  35.     print("Connected.");
  36.     sleep(0.5);
  37.     ws.send(id);
  38.     while true do
  39.       print("Waiting For Message...");
  40.       local message = ws.receive();
  41.       onMessage(message);
  42.     end
  43.   else
  44.     print("Disconnected...");
  45.     sleep(1);
  46.   end
  47. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement