修复:SSE 断档页面假死——可见心跳 + 静默看门狗自动重同步

根因:/messages 已进服务端但页面不刷新——流断档窗口内的事件(assistant_message/done 等)在 sse.js 无客户端即丢弃、不重放;EventSource 多半已自动重连,缺的是「重新拉库」。手动切会话再回来能恢复,正是 renderDetail 重拉的效果。

- server: 心跳从注释帧 `: ping` 改为具名事件 event: ping(注释帧不触发前端 JS 事件,无法做静默检测;对代理保活等价)
- 前端: connectStream 记录 state.lastSseAt + ping 监听刷新;watchdog 每 5s 判定,会话页且静默 >45s(3 个心跳周期)→ resyncSession():closeStream + GET limit + renderDetail + connectStream,即自动化「切走再切回」
- visibilitychange 回前台立即补判(后台 interval 被浏览器限频,覆盖休眠/长切走场景)
- 防抖:sseResyncing 互斥 + 每次重同步重置计时,失败按 45s 节奏重试;草稿/输入框不受影响(只重绘消息区)
This commit is contained in:
2026-09-10 20:18:19 +08:00
parent 5390ede605
commit 6ffd98b67b
2 changed files with 32 additions and 1 deletions
+2 -1
View File
@@ -85,7 +85,8 @@ router.get('/:id/stream', (req, res) => {
res.write('retry: 3000\n\n');
bus.addClient(session.id, res);
const heartbeat = setInterval(() => {
try { res.write(': ping\n\n'); } catch (_) {}
// 可见心跳:具名事件(注释帧 `: ping` 不触发前端 JS 事件,无法做静默看门狗)
try { res.write('event: ping\ndata: {}\n\n'); } catch (_) {}
}, 15000);
req.on('close', () => {
clearInterval(heartbeat);