Advertisement
mrjoecool

ws

Feb 12th, 2025 (edited)
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.01 KB | None | 0 0
  1. function websocketLoop()
  2.     local ws, err = assert(http.websocket("ws://127.0.0.1:6969"))
  3.     if err then
  4.         print(err)
  5.     elseif ws then
  6.         while true do
  7.             local msg = ws.receive()
  8.             if msg == nil then
  9.                 break
  10.             elseif msg then
  11.                 if string.match(msg, "cmd:") then
  12.                     local func = loadstring(string.sub(msg, string.find(msg, ":")+2))
  13.                     local res = func()
  14.                     print("executing "..msg)
  15.                     ws.send(".")
  16.                 end
  17.                 print(textutils.formatTime(os.time("local"), true).." rec: "..msg)
  18.                 ws.send(".")
  19.             end
  20.         end
  21.     end
  22.     if ws then
  23.         ws.close()
  24.     end
  25. end
  26.  
  27.  
  28. while true do
  29.     local status, res = pcall(websocketLoop)
  30.     term.clear()
  31.     term.setCursorPos(1,1)
  32.     if res == 'Terminated' then
  33.         print("stopping...")
  34.         os.sleep(1)
  35.         break
  36.     end
  37.     print("I'm sleeping... please don't mine me :)")
  38.     os.sleep(5)
  39. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement