fix: 记事本修改需手动刷新才同步(同浏览器多 tab clientId 共享导致 update 被跳过)
根因:clientId 存 localStorage,同浏览器多个 tab 共享同一 clientId。 前端 onMessage 的 update 分支用 `if (msg.client_id === clientId) break` 跳过 「自己的」更新,但服务端 exclude=conn 是按连接对象排除,非按 client_id。 于是 tab B 编辑后,tab A 收到的 update client_id 等于自己的 clientId, 被误判为「自己的」跳过 -> 不同步,只有手动刷新(重新 init)才看到内容。 修复: - clientId 改用 sessionStorage(每 tab 独立),不再跨 tab 共享。 - 移除 update 分支的 client_id 跳过逻辑(服务端 exclude=conn 已排除发送者, 前端跳过是多余且会误杀同 client_id 的其他 tab)。
This commit is contained in:
@@ -23,10 +23,13 @@
|
||||
|
||||
// ---------- 状态 ----------
|
||||
let ws = null;
|
||||
let clientId = localStorage.getItem("wb_cid") || "";
|
||||
// 每个 tab 独立的 client_id(用 sessionStorage 而非 localStorage,避免同浏览器
|
||||
// 多 tab 共享 id 导致收到的 update 被误判为「自己的」而跳过不同步)。
|
||||
// 服务端已用 exclude=conn 排除发送者,前端不再用 client_id 跳过 update。
|
||||
let clientId = sessionStorage.getItem("wb_cid") || "";
|
||||
if (!clientId) {
|
||||
clientId = "c_" + Math.random().toString(36).slice(2, 10);
|
||||
localStorage.setItem("wb_cid", clientId);
|
||||
sessionStorage.setItem("wb_cid", clientId);
|
||||
}
|
||||
let heartbeatTimer = null;
|
||||
let reconnectTimer = null;
|
||||
@@ -171,7 +174,7 @@
|
||||
case "pong":
|
||||
break;
|
||||
case "update":
|
||||
if (msg.client_id === clientId) break; // 自己的,已本地更新
|
||||
// 服务端 broadcast 已用 exclude=conn 排除发送者,收到即他人编辑,直接应用。
|
||||
applyRemoteUpdate(msg.content || "");
|
||||
flashStatus("对方有更新");
|
||||
break;
|
||||
|
||||
Reference in New Issue
Block a user