termchat: 断线自动重连 — 服务端常驻监听 + 客户端退避重连
- 服务端不再在接受首个连接后关闭 listener,新连接到来时替换旧连接 (替换语义同时是半开连接的恢复路径) - 客户端断线后指数退避自动重连(500ms 起步、封顶 10s),连上即重置; 支持先于服务端启动;连接阶段 5s 超时防不可达地址卡 SYN 重传 - socket 代数计数:被替换/拆除的旧 socket 的迟到事件全部判废 - 修复潜伏 bug:readline 将 socket error 转发重抛,无 listener 直接 崩进程 —— 原先"连不上报错退出"实为崩溃 - ECONNRESET 过滤(正常断开不再显示吓人报错)、同文本错误去重 (无限重连不刷屏)、TCP keepalive 30s - Phase 新增 reconnecting;UI 适配重连状态显示 - smoke 测试重写为事件驱动 4 场景(基础收发/客户端先行/服务端替换/ 服务端重启+断线排队 flush) Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
# termchat — 极简双人终端聊天
|
||||
|
||||
一个命令启动,两个人直连聊天。无用户系统、无历史存储,断线即散。
|
||||
一个命令启动,两个人直连聊天。无用户系统、无历史存储,断线自动重连。
|
||||
自带摸鱼模式:老板键一键伪装、全程不留痕迹。
|
||||
|
||||
## 用法
|
||||
@@ -66,8 +66,12 @@ Windows 上 npm 会吞掉 `-n` 短选项,dev 模式下请用位置参数或 `--n
|
||||
## 说明
|
||||
|
||||
- 协议:TCP,每行一个 JSON(`{"type":"msg","name":"…","text":"…","ts":…}`)
|
||||
- 服务端只接受第一个连入的客户端
|
||||
- **断线自动重连**:客户端断线后指数退避重连(500ms 起步、封顶 10s),
|
||||
重启对方即可恢复,无需重启自己;客户端也可以先于服务端启动,会一直等
|
||||
- 服务端常驻监听,新连接到来时替换旧连接(顺便救活半开连接);
|
||||
界面上 `reconnecting…` / `等待重连…` 即重连状态
|
||||
- 局域网直连;跨公网需要 A 侧有公网 IP 或端口映射(或后续加中转服务器)
|
||||
- 目前**明文传输**,同网段可被抓包看到内容;加密(--key)后续版本再加
|
||||
- Windows 首次 `serve` 会弹防火墙授权,允许即可
|
||||
|
||||
## 测试
|
||||
|
||||
+170
-28
@@ -1,35 +1,177 @@
|
||||
/** 不依赖 UI 的通信层冒烟测试:本机起 server + client,验证双向收发 */
|
||||
import { createSession, type ChatMessage } from "../src/net.js";
|
||||
/** 不依赖 UI 的通信层冒烟测试:本机起 server + client,验证双向收发与断线自动重连 */
|
||||
import * as net from "node:net";
|
||||
import { createSession, type ChatMessage, type Phase, type Session } from "../src/net.js";
|
||||
|
||||
const port = 19321;
|
||||
const aGot: ChatMessage[] = [];
|
||||
const bGot: ChatMessage[] = [];
|
||||
/* ---------- harness ---------- */
|
||||
|
||||
const a = createSession("server", { port, name: "A" }, {
|
||||
onMessage: (m) => aGot.push(m),
|
||||
onPhase: () => {},
|
||||
/** 事件日志:失败时 dump 全部时序,方便排障 */
|
||||
const events: string[] = [];
|
||||
const t0 = Date.now();
|
||||
const log = (s: string): void => {
|
||||
const line = `[${((Date.now() - t0) / 1000).toFixed(2)}s] ${s}`;
|
||||
events.push(line);
|
||||
console.log(` ${line}`);
|
||||
};
|
||||
|
||||
const sleep = (ms: number): Promise<void> => new Promise((r) => setTimeout(r, ms));
|
||||
|
||||
/** 取一个空闲端口(每个场景独立,避免固定端口复跑 EADDRINUSE) */
|
||||
function freePort(): Promise<number> {
|
||||
return new Promise((resolve) => {
|
||||
const srv = net.createServer();
|
||||
srv.listen(0, "127.0.0.1", () => {
|
||||
const { port } = srv.address() as net.AddressInfo;
|
||||
srv.close(() => resolve(port));
|
||||
});
|
||||
|
||||
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);
|
||||
/** 轮询等待条件成立;超时 dump 事件日志后抛错 */
|
||||
async function waitFor(desc: string, pred: () => boolean, timeoutMs = 10_000): Promise<void> {
|
||||
const deadline = Date.now() + timeoutMs;
|
||||
while (Date.now() < deadline) {
|
||||
if (pred()) {
|
||||
log(`✓ ${desc}`);
|
||||
return;
|
||||
}
|
||||
await sleep(50);
|
||||
}
|
||||
console.error(`✗ 超时等待:${desc}`);
|
||||
console.error("--- 事件日志 ---");
|
||||
for (const e of events) console.error(e);
|
||||
throw new Error(`waitFor 超时:${desc}`);
|
||||
}
|
||||
|
||||
/** 建会话并记录消息/状态,便于断言与排障 */
|
||||
interface Wrapped {
|
||||
session: Session;
|
||||
msgs: ChatMessage[];
|
||||
sysTexts: string[];
|
||||
phases: Phase[];
|
||||
}
|
||||
|
||||
function wrap(who: string, role: "server" | "client", port: number, name: string): Wrapped {
|
||||
const msgs: ChatMessage[] = [];
|
||||
const sysTexts: string[] = [];
|
||||
const phases: Phase[] = [];
|
||||
const session = createSession(role, { host: "127.0.0.1", port, name }, {
|
||||
onMessage: (m) => {
|
||||
if (m.type === "msg") {
|
||||
msgs.push(m);
|
||||
log(`${who} ⇐ ${m.name}:${m.text}`);
|
||||
} else {
|
||||
sysTexts.push(m.text);
|
||||
log(`${who} · ${m.text}`);
|
||||
}
|
||||
},
|
||||
onPhase: (p) => {
|
||||
phases.push(p);
|
||||
log(`${who} phase → ${p}`);
|
||||
},
|
||||
});
|
||||
return { session, msgs, sysTexts, phases };
|
||||
}
|
||||
|
||||
const hasMsg = (w: Wrapped, name: string, text: string): boolean =>
|
||||
w.msgs.some((m) => m.name === name && m.text === text);
|
||||
const hasSys = (w: Wrapped, s: string): boolean => w.sysTexts.some((t) => t.includes(s));
|
||||
const bothConnected = (a: Wrapped, b: Wrapped): boolean =>
|
||||
a.session.phase === "connected" && b.session.phase === "connected";
|
||||
|
||||
/* ---------- 场景(谓词一律写终态,不依赖中间顺序) ---------- */
|
||||
|
||||
async function main(): Promise<void> {
|
||||
// S0 基础回归:连接 + 双向收发
|
||||
console.log("— S0 基础回归 —");
|
||||
{
|
||||
const port = await freePort();
|
||||
const a = wrap("A", "server", port, "A");
|
||||
const b = wrap("B", "client", port, "B");
|
||||
await waitFor("S0 双方 connected", () => bothConnected(a, b));
|
||||
b.session.send("你好,A!");
|
||||
a.session.send("收到,B");
|
||||
await waitFor("S0 双向送达", () => hasMsg(a, "B", "你好,A!") && hasMsg(b, "A", "收到,B"));
|
||||
a.session.close();
|
||||
b.session.close();
|
||||
}
|
||||
|
||||
// S1 客户端先行:服务端没起,一直重试不退出;服务端后启动即连上
|
||||
console.log("— S1 客户端先行 —");
|
||||
{
|
||||
const port = await freePort();
|
||||
const b = wrap("B", "client", port, "B");
|
||||
await sleep(1500); // 回环 ECONNREFUSED 即时,期间已完成多次失败重试
|
||||
if (b.phases.includes("closed")) throw new Error("S1:客户端不应进 closed");
|
||||
if (b.session.phase !== "waiting") throw new Error("S1:未连上过应停在 waiting");
|
||||
const fails = b.sysTexts.filter((t) => t.startsWith("Connection failed")).length;
|
||||
if (fails !== 1) throw new Error(`S1:同文本错误应去重为 1 次,实际 ${fails}`);
|
||||
log("S1 重试中,错误提示已去重为 1 次");
|
||||
const a = wrap("A", "server", port, "A");
|
||||
await waitFor("S1 服务端后启动,双方 connected", () => bothConnected(a, b), 15_000);
|
||||
b.session.send("迟到的问候");
|
||||
await waitFor("S1 送达", () => hasMsg(a, "B", "迟到的问候"));
|
||||
a.session.close();
|
||||
b.session.close();
|
||||
}
|
||||
|
||||
// S2 服务端替换 + 客户端重连:第三个连接插进来,服务端换掉旧连接
|
||||
console.log("— S2 服务端替换 + 客户端重连 —");
|
||||
{
|
||||
const port = await freePort();
|
||||
const a = wrap("A", "server", port, "A");
|
||||
const b = wrap("B", "client", port, "B");
|
||||
await waitFor("S2 双方 connected", () => bothConnected(a, b));
|
||||
b.session.send("替换前的消息");
|
||||
await waitFor("S2 A 收到首条", () => hasMsg(a, "B", "替换前的消息"));
|
||||
const c = net.connect({ host: "127.0.0.1", port });
|
||||
c.on("error", () => {}); // 挂 no-op,防 unhandled error 崩测试
|
||||
// B 被服务端替换 → 断线进 reconnecting(这就是替换语义的可观测证明)
|
||||
await waitFor("S2 B 被替换进 reconnecting", () => b.session.phase === "reconnecting");
|
||||
if (a.session.phase !== "connected") throw new Error("S2:A 此刻应仍 connected(挂着新连接)");
|
||||
c.destroy(); // 裸连接退场,给 B 让路
|
||||
await waitFor("S2 B 重连成功,双方 connected", () => bothConnected(a, b), 15_000);
|
||||
if (!hasSys(a, "Peer reconnected")) throw new Error("S2:A 应收到 Peer reconnected");
|
||||
b.session.send("重连后的消息");
|
||||
a.session.send("收到,欢迎回来");
|
||||
await waitFor("S2 双向送达", () => hasMsg(a, "B", "重连后的消息") && hasMsg(b, "A", "收到,欢迎回来"));
|
||||
a.session.close();
|
||||
b.session.close();
|
||||
}
|
||||
|
||||
// S3 服务端重启 + 断线排队:服务端退出重起,排队消息重连后送达
|
||||
console.log("— S3 服务端重启 + 断线排队 —");
|
||||
{
|
||||
const port = await freePort();
|
||||
const a = wrap("A", "server", port, "A");
|
||||
const b = wrap("B", "client", port, "B");
|
||||
await waitFor("S3 双方 connected", () => bothConnected(a, b));
|
||||
a.session.close(); // 模拟服务端退出
|
||||
await waitFor("S3 B 进 reconnecting", () => b.session.phase === "reconnecting");
|
||||
b.session.send("offline-1");
|
||||
b.session.send("offline-2"); // 此刻无 socket,进 queue
|
||||
await sleep(300); // 给端口释放留时间
|
||||
const a2 = wrap("A2", "server", port, "A");
|
||||
await waitFor("S3 B 重连上新服务端", () => bothConnected(a2, b), 15_000);
|
||||
await waitFor("S3 排队消息 flush 到 A2", () => hasMsg(a2, "B", "offline-1") && hasMsg(a2, "B", "offline-2"));
|
||||
a2.session.send("新链路问候");
|
||||
await waitFor("S3 新链路送达", () => hasMsg(b, "A", "新链路问候"));
|
||||
a2.session.close();
|
||||
b.session.close();
|
||||
}
|
||||
|
||||
console.log("✅ 全部 4 个场景通过");
|
||||
}
|
||||
|
||||
/* 全局看门狗:任何场景挂死则 dump 日志退出 */
|
||||
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);
|
||||
console.error("✗ 全局超时(60s)");
|
||||
for (const e of events) console.error(e);
|
||||
process.exit(1);
|
||||
}, 60_000);
|
||||
|
||||
main()
|
||||
.then(() => process.exit(0))
|
||||
.catch((err: unknown) => {
|
||||
console.error(`❌ ${err instanceof Error ? err.message : err}`);
|
||||
process.exit(1);
|
||||
});
|
||||
|
||||
+132
-17
@@ -10,7 +10,14 @@ export interface ChatMessage {
|
||||
ts: number;
|
||||
}
|
||||
|
||||
export type Phase = "waiting" | "connected" | "closed";
|
||||
/**
|
||||
* 会话阶段:
|
||||
* - waiting 服务端等待对方连入 / 客户端连接中(从未连上过的失败也停在这里)
|
||||
* - connected 已连接
|
||||
* - reconnecting 客户端连上过又断开,退避重连中(服务端不会进此状态)
|
||||
* - closed 终态(手动 close 或 listen 失败)
|
||||
*/
|
||||
export type Phase = "waiting" | "connected" | "reconnecting" | "closed";
|
||||
|
||||
export interface SessionHandlers {
|
||||
onMessage(m: ChatMessage): void;
|
||||
@@ -29,10 +36,18 @@ export interface Session {
|
||||
readonly phase: Phase;
|
||||
}
|
||||
|
||||
/** 客户端重连退避:500ms 起,指数增长,封顶 10s */
|
||||
const RETRY_BASE_MS = 500;
|
||||
const RETRY_MAX_MS = 10_000;
|
||||
/** 连接超时:超时就 destroy 重试,免得不可达地址上卡 ~21s 的 SYN 重传 */
|
||||
const CONNECT_TIMEOUT_MS = 5_000;
|
||||
/** TCP keepalive:半开连接的探测(主恢复路径是替换/重连,这只是让状态行别谎报) */
|
||||
const KEEPALIVE_MS = 30_000;
|
||||
|
||||
/**
|
||||
* 建立一条双人会话:
|
||||
* - server: 监听端口,等对方连入(只接受第一个连接)
|
||||
* - client: 主动连接对方
|
||||
* - server: 监听端口,常驻等待对方连入;新连接到来时替换旧连接(半开连接的恢复路径)
|
||||
* - client: 主动连接对方,断线后指数退避自动重连,可以先于服务端启动
|
||||
* 协议:每行一个 JSON
|
||||
*/
|
||||
export function createSession(
|
||||
@@ -42,8 +57,15 @@ export function createSession(
|
||||
): Session {
|
||||
let socket: net.Socket | null = null;
|
||||
let server: net.Server | null = null;
|
||||
let rl: ReturnType<typeof createInterface> | null = null;
|
||||
let phase: Phase = "waiting";
|
||||
const queue: string[] = [];
|
||||
let gen = 0; // socket 代数:被替换/拆除的旧 socket 的事件凭它判废
|
||||
let stopped = false; // 手动 close() 后一切活动停止
|
||||
let everConnected = false; // 是否连上过(首次/重连文案 + waiting/reconnecting 语义)
|
||||
let retryTimer: NodeJS.Timeout | null = null; // 单一重连定时器(单飞,防重复调度)
|
||||
let retryDelay = RETRY_BASE_MS;
|
||||
let lastErrText = ""; // 同文本错误只提示一次,防无限重连刷屏
|
||||
|
||||
const sys = (text: string): void =>
|
||||
handlers.onMessage({ type: "sys", name: "", text, ts: Date.now() });
|
||||
@@ -53,14 +75,33 @@ export function createSession(
|
||||
phase = p;
|
||||
handlers.onPhase(p);
|
||||
if (p === "connected") {
|
||||
everConnected = true;
|
||||
for (const payload of queue.splice(0)) socket?.write(payload);
|
||||
}
|
||||
};
|
||||
|
||||
const wire = (sock: net.Socket): void => {
|
||||
/** 拆除当前 socket(替换旧连接 / close() 用):先加代数,旧 socket 的事件必然过期 */
|
||||
const teardownSocket = (): void => {
|
||||
gen++;
|
||||
const s = socket;
|
||||
socket = null;
|
||||
const r = rl;
|
||||
rl = null;
|
||||
r?.close();
|
||||
s?.destroy();
|
||||
};
|
||||
|
||||
/** 挂上新 socket:事件回调凭代数 g 判废,过期即忽略 */
|
||||
const wire = (sock: net.Socket): number => {
|
||||
const g = ++gen;
|
||||
socket = sock;
|
||||
const rl = createInterface({ input: sock });
|
||||
sock.setKeepAlive(true, KEEPALIVE_MS);
|
||||
rl = createInterface({ input: sock });
|
||||
// readline 会把 socket 的 error 转发到 Interface 上再抛,没 listener 会崩整个进程
|
||||
// (socket 层已处理并去重提示,这里 no-op 即可)
|
||||
rl.on("error", () => {});
|
||||
rl.on("line", (line) => {
|
||||
if (g !== gen) return; // 迟到的旧 socket 数据,丢弃
|
||||
const s = line.trim();
|
||||
if (!s) return;
|
||||
try {
|
||||
@@ -70,31 +111,98 @@ export function createSession(
|
||||
handlers.onMessage({ type: "msg", name: "peer", text: s, ts: Date.now() });
|
||||
}
|
||||
});
|
||||
sock.on("error", (err) => sys(`Connection error: ${err.message}`));
|
||||
sock.on("close", () => setPhase("closed"));
|
||||
sock.on("error", (err) => {
|
||||
if (g !== gen) return;
|
||||
// ECONNRESET 是常态(旧连接被替换/对端被杀),交给 close 走正常断开路径
|
||||
if ((err as NodeJS.ErrnoException).code === "ECONNRESET") return;
|
||||
if (err.message !== lastErrText) {
|
||||
lastErrText = err.message;
|
||||
sys(`Connection failed: ${err.message}`);
|
||||
}
|
||||
});
|
||||
sock.on("close", () => {
|
||||
if (g !== gen) return; // 被替换/拆除的旧 socket
|
||||
if (socket === sock) {
|
||||
socket = null;
|
||||
rl?.close();
|
||||
rl = null;
|
||||
}
|
||||
if (stopped || phase === "closed") return;
|
||||
if (role === "server") {
|
||||
setPhase("waiting");
|
||||
sys("Peer disconnected, waiting for reconnect…");
|
||||
} else {
|
||||
// 从未连上过的失败停在 waiting(不算"重连");连上过才进 reconnecting
|
||||
if (everConnected) setPhase("reconnecting");
|
||||
scheduleRetry();
|
||||
}
|
||||
});
|
||||
return g;
|
||||
};
|
||||
|
||||
/** 安排一次重连:单飞 guard 收敛 error+close 连发、超时与 close 的竞态 */
|
||||
const scheduleRetry = (): void => {
|
||||
if (stopped || phase === "closed") return;
|
||||
if (retryTimer !== null) return;
|
||||
const delay = retryDelay;
|
||||
retryDelay = Math.min(retryDelay * 2, RETRY_MAX_MS);
|
||||
retryTimer = setTimeout(() => {
|
||||
retryTimer = null;
|
||||
attemptConnect();
|
||||
}, delay);
|
||||
};
|
||||
|
||||
const attemptConnect = (): void => {
|
||||
if (stopped) return;
|
||||
retryTimer = null;
|
||||
const sock = net.connect({ host: opts.host ?? "127.0.0.1", port: opts.port });
|
||||
wire(sock); // 先挂好 error/close 监听,事件最早下个 tick 才来
|
||||
const connectTimer = setTimeout(
|
||||
() => sock.destroy(new Error("connect timeout")),
|
||||
CONNECT_TIMEOUT_MS
|
||||
);
|
||||
sock.on("connect", () => {
|
||||
clearTimeout(connectTimer);
|
||||
retryDelay = RETRY_BASE_MS; // 成功即重置退避
|
||||
lastErrText = ""; // 允许下次断线后再报一次错误
|
||||
const first = !everConnected; // 快照必须在 setPhase 之前
|
||||
setPhase("connected");
|
||||
sys(
|
||||
first
|
||||
? `Connected to ${opts.host}:${opts.port}`
|
||||
: `Reconnected to ${opts.host}:${opts.port}`
|
||||
);
|
||||
});
|
||||
// 失败/超时统一走 wire 挂的 close handler → scheduleRetry,单出口
|
||||
};
|
||||
|
||||
if (role === "server") {
|
||||
server = net.createServer((sock) => {
|
||||
server?.close(); // 只接受第一个连入的人
|
||||
if (stopped) {
|
||||
sock.destroy(); // 与 close() 同 tick 进来的新连接,别在已关会话上复活状态
|
||||
return;
|
||||
}
|
||||
if (socket) teardownSocket(); // 替换旧连接
|
||||
wire(sock);
|
||||
lastErrText = "";
|
||||
const first = !everConnected; // 快照必须在 setPhase 之前
|
||||
setPhase("connected");
|
||||
sys("Peer connected");
|
||||
// connected→connected 的替换不触发 setPhase 内的 flush,显式兜底
|
||||
for (const payload of queue.splice(0)) sock.write(payload);
|
||||
sys(first ? "Peer connected" : "Peer reconnected");
|
||||
});
|
||||
server.on("error", (err) => {
|
||||
if (stopped) return;
|
||||
sys(`Listen failed: ${err.message}`);
|
||||
stopped = true; // listen 失败是终态,不自动重试
|
||||
teardownSocket();
|
||||
setPhase("closed");
|
||||
});
|
||||
server.listen(opts.port, () => {
|
||||
sys(`Waiting for peer on 0.0.0.0:${opts.port}…`);
|
||||
});
|
||||
} else {
|
||||
const sock = net.connect({ host: opts.host ?? "127.0.0.1", port: opts.port });
|
||||
wire(sock);
|
||||
sock.on("connect", () => {
|
||||
setPhase("connected");
|
||||
sys(`Connected to ${opts.host}:${opts.port}`);
|
||||
});
|
||||
attemptConnect(); // 启动即首次尝试,失败自动退避重试
|
||||
}
|
||||
|
||||
return {
|
||||
@@ -102,11 +210,18 @@ export function createSession(
|
||||
const payload =
|
||||
JSON.stringify({ type: "msg", name: opts.name, text, ts: Date.now() }) + "\n";
|
||||
if (phase === "connected") socket?.write(payload);
|
||||
else if (phase === "waiting") queue.push(payload);
|
||||
else if (phase === "waiting" || phase === "reconnecting") queue.push(payload);
|
||||
// closed 丢弃(终态)
|
||||
},
|
||||
close(): void {
|
||||
if (stopped) return; // 幂等
|
||||
stopped = true;
|
||||
if (retryTimer !== null) {
|
||||
clearTimeout(retryTimer);
|
||||
retryTimer = null;
|
||||
}
|
||||
teardownSocket();
|
||||
server?.close();
|
||||
socket?.end();
|
||||
setPhase("closed");
|
||||
},
|
||||
get phase(): Phase {
|
||||
|
||||
+20
-5
@@ -65,6 +65,9 @@ function App({ role, host, port, name }: UIOptions) {
|
||||
const [messages, setMessages] = useState<ChatMessage[]>([]);
|
||||
const [input, setInput] = useState("");
|
||||
const [phase, setPhase] = useState<Phase>("waiting");
|
||||
// 服务端 waiting 的两种含义(初始等待 vs 对方走了等重连)靠它区分;
|
||||
// 客户端天然由 phase === "reconnecting" 表达,不需要它
|
||||
const [everConnected, setEverConnected] = useState(false);
|
||||
const [cover, setCover] = useState(false); // 老板键开关
|
||||
const [plain, setPlain] = useState(true); // 低调渲染开关
|
||||
const [cursorOn, setCursorOn] = useState(true); // 自绘光标闪烁
|
||||
@@ -115,7 +118,10 @@ function App({ role, host, port, name }: UIOptions) {
|
||||
];
|
||||
});
|
||||
},
|
||||
onPhase: (p) => setPhase(p),
|
||||
onPhase: (p) => {
|
||||
if (p === "connected") setEverConnected(true);
|
||||
setPhase(p);
|
||||
},
|
||||
}
|
||||
);
|
||||
sessionRef.current = session;
|
||||
@@ -186,11 +192,15 @@ function App({ role, host, port, name }: UIOptions) {
|
||||
|
||||
if (cover) return <Cover />;
|
||||
|
||||
// 低调模式 footer:连接后显示原版权限模式行,等待/断开时显示状态
|
||||
// 低调模式 footer:连接后显示原版权限模式行,等待/重连/断开时显示状态
|
||||
const statusPlain =
|
||||
phase === "waiting"
|
||||
phase === "reconnecting"
|
||||
? `reconnecting to ${host}:${port}…`
|
||||
: phase === "waiting"
|
||||
? role === "server"
|
||||
? `waiting for peer on 0.0.0.0:${port}…`
|
||||
? everConnected
|
||||
? `peer left · waiting for reconnect on 0.0.0.0:${port}…`
|
||||
: `waiting for peer on 0.0.0.0:${port}…`
|
||||
: `connecting to ${host}:${port}…`
|
||||
: "disconnected · ctrl+c to exit";
|
||||
const footerPlain =
|
||||
@@ -207,12 +217,17 @@ function App({ role, host, port, name }: UIOptions) {
|
||||
const statusText =
|
||||
phase === "connected"
|
||||
? `已连接 · 你是「${name}」`
|
||||
: phase === "reconnecting"
|
||||
? "连接断开,自动重连中…"
|
||||
: phase === "waiting"
|
||||
? role === "server"
|
||||
? `等待对方连接 0.0.0.0:${port} …`
|
||||
? everConnected
|
||||
? `对方已断开,等待重连 0.0.0.0:${port} …`
|
||||
: `等待对方连接 0.0.0.0:${port} …`
|
||||
: `正在连接 ${host}:${port} …`
|
||||
: "连接已断开,按 Ctrl+C 退出";
|
||||
|
||||
// reconnecting/closed 都落 red,waiting 黄,connected 绿
|
||||
const borderColor =
|
||||
phase === "connected" ? "green" : phase === "waiting" ? "yellow" : "red";
|
||||
|
||||
|
||||
Reference in New Issue
Block a user