Advertisement
Elkemental

startup.lua

Jan 19th, 2025
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.39 KB | None | 0 0
  1. local ws;
  2.  
  3. local function init(url)
  4.     if ws then
  5.         ws.close();
  6.         ws = nil;
  7.     end
  8.  
  9.     local connection = http.websocket(url);
  10.  
  11.     if connection then
  12.         ws = connection;
  13.         print("Connection opened!");
  14.     else
  15.         print("Failed to connect to: " .. url);
  16.     end
  17. end
  18.  
  19. local function cleanup()
  20.     print("Exiting...");
  21.     ws.close();
  22.     ws = nil;
  23.     shell.exit()
  24. end
  25.  
  26. local function executeInstruction(s)
  27.     if s == "::close" then
  28.         print("Connection closed.");
  29.         cleanup();
  30.         return 0;
  31.     elseif s == "::forward" then
  32.         forward();
  33.         return 0;
  34.     elseif s == "::back" then
  35.         back();
  36.         return 0;
  37.     elseif s == "::left" then
  38.         left();
  39.         return 0;
  40.     elseif s == "::right" then
  41.         right();
  42.         return 0;
  43.     elseif s == "::up" then
  44.         up();
  45.         return 0;
  46.     elseif s == "::down" then
  47.         down();
  48.         return 0;
  49.     end
  50.     return -1;
  51. end
  52.  
  53. init("ws://localhost:8080");
  54.  
  55. while true do
  56.     local event = os.pullEventRaw(); if event == "terminate" then print("Program terminated"); cleanup(); break; end;
  57.  
  58.     local message = ws.receive();
  59.     if message then
  60.         if executeInstruction(message) == -1 then
  61.             print(message);
  62.         end
  63.     else
  64.         print("Connection closed.");
  65.         cleanup();
  66.         break;
  67.     end
  68. end
  69.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement