From 25133f90ed555a3a8ad1723bf160400572bd0b0f Mon Sep 17 00:00:00 2001 From: kicer Date: Sun, 15 Nov 2026 15:19:19 +0800 Subject: [PATCH] =?UTF-8?q?fix(tunnel):=20SSE=20=E6=96=AD=E5=BC=80?= =?UTF-8?q?=E6=97=B6=E6=B2=A1=E9=80=9A=E7=9F=A5=E5=AE=B6=E9=87=8C=E5=81=9C?= =?UTF-8?q?=E6=8E=89=E4=B8=8A=E6=B8=B8=E8=AF=B7=E6=B1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit server.js 原来用 req.on('aborted') 发 abort,但 SSE 是 GET 无 body, 这个事件只在传输请求体时触发(Node 也已废弃它),实际永远不触发。 后果:前端切会话时 es.close() 关掉旧 SSE,隧道这边收不到 abort,client 继续 读流、继续往 WS 灌数据,core 那边的 SSE 连接也没断。切到正在运行的会话就是 新旧两条流同时推,把单条 WS 占满,后发的 detail 请求只能排队 —— 表现为白屏 1-2 秒。 改挂 res.on('close'):正常结束时 pending 已删会直接返回,只在客户端提前断开时发 abort。 --- tunnel/server.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/tunnel/server.js b/tunnel/server.js index c1a3f4a..7a4506e 100644 --- a/tunnel/server.js +++ b/tunnel/server.js @@ -123,9 +123,12 @@ function handleRequest(req, res, fixedService) { client.send(JSON.stringify({ id, op: 'body', b64: chunk.toString('base64') })); }); req.on('end', () => { if (!overflow) client.send(JSON.stringify({ id, op: 'end' })); }); - // 手机/浏览器提前断开:让家里把上游请求也停掉,别白跑 - req.on('aborted', () => { - if (!pending.has(id)) return; + // 客户端提前断开:让家里把上游请求也停掉,别白跑。 + // 必须挂在 res 上:SSE 是 GET 无 body,req 的 'aborted' 只在传 body 时触发(且已废弃)。 + // 漏掉这条时,前端关掉 SSE 后 client 会继续读流、继续往 WS 灌数据,切会话时 + // 新旧两条流同时推,把单条 WS 占满,后发的 detail 请求只能排队。 + res.on('close', () => { + if (!pending.has(id)) return; // 正常结束时 pending 已删 client.send(JSON.stringify({ id, op: 'abort' })); pending.delete(id); });