Advertisement
gravityio

HTTP-EDIT.LUA

Nov 3rd, 2021
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.97 KB | None | 0 0
  1. local ws = require("websocket_client");
  2. local event = require("event");
  3. local io = require("io");
  4.  
  5. local cl;
  6.  
  7. local function save(message)
  8.   local fileName, content = message:match("(%S+) (.+)");
  9.   local file = io.open(fileName, "w");
  10.   file:write(content);
  11.   file:close();
  12. end
  13.  
  14. local function sync(message)
  15.   local fileName = message;
  16.   local file = io.open(fileName);
  17.   local content = "";
  18.   while true do
  19.     if (not file) then break end
  20.     local buffer = file:read(256);
  21.     if (not buffer) then break end
  22.     content = content..buffer;
  23.   end
  24.   cl:send(content);
  25.   file:close();
  26. end
  27.  
  28. local function onMessage(e, msg)
  29.   print("Received Message...");
  30.   local op, message = msg:match("(%S+) (.+)");
  31.   if (op == "sync") then sync(message);
  32.   elseif (op == "save") then save(message) end
  33. end
  34.  
  35. cl = ws.create(onMessage);
  36.  
  37. cl:connect("192.168.0.69", 8080, "/", false);
  38.  
  39. while true do
  40.   local e = {event.pull()};
  41.   if (e[1] == "interrupted") then break end
  42. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement