36 lines
1.0 KiB
TypeScript
36 lines
1.0 KiB
TypeScript
|
|
/** 不依赖 UI 的通信层冒烟测试:本机起 server + client,验证双向收发 */
|
||
|
|
import { createSession, type ChatMessage } from "../src/net.js";
|
||
|
|
|
||
|
|
const port = 19321;
|
||
|
|
const aGot: ChatMessage[] = [];
|
||
|
|
const bGot: ChatMessage[] = [];
|
||
|
|
|
||
|
|
const a = createSession("server", { port, name: "A" }, {
|
||
|
|
onMessage: (m) => aGot.push(m),
|
||
|
|
onPhase: () => {},
|
||
|
|
});
|
||
|
|
|
||
|
|
const b = createSession("client", { host: "127.0.0.1", port, name: "B" }, {
|
||
|
|
onMessage: (m) => bGot.push(m),
|
||
|
|
onPhase: () => {},
|
||
|
|
});
|
||
|
|
|
||
|
|
setTimeout(() => {
|
||
|
|
b.send("你好,A!");
|
||
|
|
a.send("收到,B");
|
||
|
|
}, 300);
|
||
|
|
|
||
|
|
setTimeout(() => {
|
||
|
|
const texts = (arr: ChatMessage[]) =>
|
||
|
|
arr.filter((m) => m.type === "msg").map((m) => `${m.name}:${m.text}`);
|
||
|
|
const aMsgs = texts(aGot);
|
||
|
|
const bMsgs = texts(bGot);
|
||
|
|
console.log("A 收到:", aMsgs);
|
||
|
|
console.log("B 收到:", bMsgs);
|
||
|
|
const ok = aMsgs.includes("B:你好,A!") && bMsgs.includes("A:收到,B");
|
||
|
|
console.log(ok ? "✅ 双向通信正常" : "❌ 测试失败");
|
||
|
|
a.close();
|
||
|
|
b.close();
|
||
|
|
process.exit(ok ? 0 : 1);
|
||
|
|
}, 1200);
|