feat: 学习状态改为内存快照,页面连接每秒自取并自动重连

This commit is contained in:
2026-11-17 17:36:22 +08:00
parent 42b29fb4d6
commit 4d1a90c321
11 changed files with 675 additions and 256 deletions
+99 -37
View File
@@ -689,45 +689,92 @@ function startStudyFallback() {
type PingSocket = WebSocket & { ping?: number };
// 状态没变时服务器也会每 5 秒发一帧。超过 15 秒一帧都没有,说明连接其实已经断了。
const STUDY_STALE_MS = 15000;
const STUDY_CONNECT_MS = 8000;
const STUDY_RETRY_MAX_MS = 10000;
let studyFrameAt = 0;
let studyDialAt = 0;
let studyWatch = 0;
function clearSocketPing(ws: PingSocket) {
if (!ws.ping) return;
window.clearInterval(ws.ping);
ws.ping = 0;
}
function releaseStudySocket() {
const ws = studySocket;
studySocket = null;
if (!ws) return;
clearSocketPing(ws);
ws.onopen = null;
ws.onclose = null;
ws.onerror = null;
ws.onmessage = null;
try {
ws.close();
} catch { /* 已经关了 */ }
}
function stopStudySocket() {
studyClosed = true;
if (studyReconnect) {
window.clearTimeout(studyReconnect);
studyReconnect = 0;
}
stopStudyFallback();
const ws = studySocket;
studySocket = null;
if (ws) {
clearSocketPing(ws);
ws.onclose = null;
ws.onerror = null;
ws.onmessage = null;
ws.close();
if (studyWatch) {
window.clearInterval(studyWatch);
studyWatch = 0;
}
stopStudyFallback();
releaseStudySocket();
}
function scheduleStudyReconnect() {
if (studyClosed || studyReconnect) return;
const delay = studyBackoff;
studyBackoff = Math.min(10000, delay * 2);
const delay = Math.round(studyBackoff * (0.8 + Math.random() * 0.4));
studyBackoff = Math.min(STUDY_RETRY_MAX_MS, studyBackoff * 2);
studyReconnect = window.setTimeout(() => {
studyReconnect = 0;
connectStudySocket();
}, delay);
}
// 连接没了:先每 3 秒查一次顶着,同时按 1、2、4 秒直到 10 秒的间隔一直重连,连上为止。
function retryStudySocket() {
releaseStudySocket();
if (studyClosed) return;
startStudyFallback();
scheduleStudyReconnect();
}
function watchStudySocket() {
if (studyClosed) return;
const ws = studySocket;
if (!ws) {
scheduleStudyReconnect();
return;
}
const now = Date.now();
if (ws.readyState === WebSocket.CONNECTING) {
if (now - studyDialAt > STUDY_CONNECT_MS) retryStudySocket();
return;
}
if (ws.readyState === WebSocket.OPEN) {
if (now - studyFrameAt > STUDY_STALE_MS) retryStudySocket();
return;
}
retryStudySocket();
}
function connectStudySocket() {
if (studyClosed) return;
if (!studyWatch) studyWatch = window.setInterval(watchStudySocket, 3000);
if (studySocket && (studySocket.readyState === WebSocket.OPEN || studySocket.readyState === WebSocket.CONNECTING)) {
return;
}
releaseStudySocket();
let ws: WebSocket;
try {
ws = new WebSocket(studySocketUrl());
@@ -738,41 +785,37 @@ function connectStudySocket() {
}
const socket = ws as PingSocket;
studySocket = socket;
let welcomed = false;
studyDialAt = Date.now();
studyFrameAt = studyDialAt;
socket.onmessage = (event) => {
if (!welcomed) {
welcomed = true;
studyBackoff = 1000;
}
studyFrameAt = Date.now();
studyBackoff = 1000;
try {
const data = JSON.parse(String(event.data));
if (data && typeof data === "object") applyStudyControl(data as Record<string, unknown>);
} catch { /* ignore */ }
};
socket.onopen = () => {
studyFrameAt = Date.now();
stopStudyFallback();
void loadStudyControl();
clearSocketPing(socket);
socket.ping = window.setInterval(() => {
if (socket.readyState === WebSocket.OPEN) socket.send("ping");
if (socket.readyState !== WebSocket.OPEN) return;
try {
socket.send("ping");
} catch { /* 下一轮检查会重连 */ }
}, 8000);
};
socket.onclose = (event) => {
clearSocketPing(socket);
if (studySocket !== socket) return;
studySocket = null;
if (event.code === 4401 || event.code === 4403) {
studyClosed = true;
stopStudyFallback();
stopStudySocket();
return;
}
if (!studyClosed) {
startStudyFallback();
scheduleStudyReconnect();
}
retryStudySocket();
};
socket.onerror = () => {
socket.close();
if (studySocket === socket) retryStudySocket();
};
}
@@ -797,21 +840,40 @@ watch(proxyOpen, (open, wasOpen) => {
let lastWake = 0;
function reconnectStudyNow() {
if (studyClosed) return;
if (studyReconnect) {
window.clearTimeout(studyReconnect);
studyReconnect = 0;
}
studyBackoff = 1000;
releaseStudySocket();
void loadStudyControl();
connectStudySocket();
}
// 回到页面时:连接还在就要一帧最新的,不在就不等重连间隔,马上连。
function studyWake() {
const now = Date.now();
if (now - lastWake < 800) return;
lastWake = now;
if (studyClosed) return;
void loadStudyControl();
if (!studySocket || studySocket.readyState === WebSocket.CLOSING || studySocket.readyState === WebSocket.CLOSED) {
connectStudySocket();
const ws = studySocket;
if (ws && ws.readyState === WebSocket.OPEN) {
try {
ws.send("ping");
return;
} catch { /* 发不出去,按断开处理 */ }
} else if (ws && ws.readyState === WebSocket.CONNECTING) {
return;
}
if (studySocket.readyState === WebSocket.OPEN) {
try {
studySocket.send("ping");
} catch { /* ignore */ }
}
reconnectStudyNow();
}
// 网络从断开恢复后,原来的连接多半已经失效,直接换一条新的。
function studyOnline() {
lastWake = Date.now();
reconnectStudyNow();
}
function onStudyVisible() {
@@ -859,14 +921,14 @@ onMounted(() => {
connectStudySocket();
document.addEventListener("visibilitychange", onStudyVisible);
window.addEventListener("focus", studyWake);
window.addEventListener("online", studyWake);
window.addEventListener("online", studyOnline);
window.addEventListener("pageshow", studyWake);
});
onUnmounted(() => {
document.removeEventListener("visibilitychange", onStudyVisible);
window.removeEventListener("focus", studyWake);
window.removeEventListener("online", studyWake);
window.removeEventListener("online", studyOnline);
window.removeEventListener("pageshow", studyWake);
stopStudySocket();
stopStudyTick();