cc-libs/tools/mcp-bridge/test-integration/lua/multi-echo-client.lua

45 lines
1.2 KiB
Lua

-- Integration-test helper: open two websocket connections in parallel, each
-- presenting itself as a distinct logical computer. Used by probe-multi.
local args = {...};
local urlBase = args[1] or "ws://127.0.0.1:2001";
local function connect(id, label, duration)
local url = urlBase .. "/?id=" .. id;
local ws, err = http.websocket(url);
if not ws then
print("websocket failed (" .. id .. "): " .. tostring(err));
return;
end
ws.send(textutils.serializeJSON({
type = "hello",
computerId = id,
computerLabel = label,
}));
local deadline = os.epoch("utc") + duration * 1000;
while os.epoch("utc") < deadline do
local msg = ws.receive(0.3);
if msg then
local frame = textutils.unserializeJSON(msg);
if frame and frame.type == "request" then
ws.send(textutils.serializeJSON({
type = "response",
id = frame.id,
ok = true,
result = "pong from " .. id .. " (Label: " .. label .. ")",
}));
end
end
end
pcall(function() ws.close(); end);
end
parallel.waitForAll(
function() connect(1001, "echo-A", 5); end,
function() connect(1002, "echo-B", 5); end
);
os.shutdown();