fix: 不让空的或上月学时盖住首页,会话失效后停止重连

This commit is contained in:
2026-11-16 02:48:38 +08:00
parent 252bfbf60b
commit 8706b8f447
3 changed files with 24 additions and 4 deletions
+16 -3
View File
@@ -472,7 +472,7 @@ function applyStudyControl(payload: Record<string, unknown>) {
message: typeof payload.message === "string" ? payload.message : "",
workers: asWorkers(payload.workers),
};
if (typeof payload.month_hours === "string" && summary.value) {
if (typeof payload.month_hours === "string" && payload.month_hours && summary.value) {
summary.value = { ...summary.value, month_hours: payload.month_hours };
}
}
@@ -480,6 +480,10 @@ function applyStudyControl(payload: Record<string, unknown>) {
async function loadStudyControl() {
try {
const res = await fetch("/api/settings/study-control");
if (res.status === 401) {
stopStudySocket();
return;
}
const data = await res.json().catch(() => ({}));
if (res.ok) applyStudyControl(data);
} catch { /* 静默 */ }
@@ -555,14 +559,18 @@ function connectStudySocket() {
return;
}
studySocket = ws;
let welcomed = false;
ws.onmessage = (event) => {
if (!welcomed) {
welcomed = true;
studyBackoff = 1000;
}
try {
const data = JSON.parse(String(event.data));
if (data && typeof data === "object") applyStudyControl(data as Record<string, unknown>);
} catch { /* ignore */ }
};
ws.onopen = () => {
studyBackoff = 1000;
stopStudyFallback();
void loadStudyControl();
if (studyPing) window.clearInterval(studyPing);
@@ -570,12 +578,17 @@ function connectStudySocket() {
if (ws.readyState === WebSocket.OPEN) ws.send("ping");
}, 8000);
};
ws.onclose = () => {
ws.onclose = (event) => {
if (studyPing) {
window.clearInterval(studyPing);
studyPing = 0;
}
if (studySocket === ws) studySocket = null;
if (event.code === 4401 || event.code === 4403) {
studyClosed = true;
stopStudyFallback();
return;
}
if (!studyClosed) {
startStudyFallback();
scheduleStudyReconnect();