Advertisement
Elkemental

test.lua

Jan 19th, 2025
40
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 0.76 KB | None | 0 0
  1. local websocket = require('websocket.client').sync;
  2.  
  3.  
  4. local monitor = peripheral.find("monitor");
  5.  
  6. local function printM(string)
  7.     monitor.write(string);
  8. end
  9.  
  10.  
  11. local ws;
  12.  
  13. local function init()
  14.     if ws then
  15.         ws:close();
  16.         ws = nil;
  17.     end
  18.  
  19.     ws = websocket();
  20.     local ok, err = ws:connect("ws://localhost:8080");
  21.     if not ok then
  22.         print("Connection error: " .. (err or "Unknown error"));
  23.         return;
  24.     end
  25.  
  26.     print('Connection Opened!');
  27. end
  28.  
  29. init();
  30.  
  31. while true do
  32.     local message, err = ws:receive();
  33.     if message then
  34.         printM(message);
  35.     elseif err == "closed" then
  36.         print("Connection Closed");
  37.         ws = nil;
  38.         break;
  39.     else
  40.         print("error" .. err);
  41.     end
  42. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement