feat(online): 停留>1min未登录访客计在线(前端60s首发+30s心跳,relay90s内存窗口,Admin usage页30s刷新展示)

This commit is contained in:
jinghui committed 2026-11-12 12:52:47 +08:00
1 parent cd4b324740
commit c6341ea35b
3 files changed
+52 -1

No files matched your search

+40
View File
@@ -273,3 +273,43 @@ export function reportToolUsage(path: string): void {
// 统计失败绝不影响功能
}
}
// ---- 在线统计心跳:打开满 1 分钟才开始上报;仅未登录访客被统计 ----
const ONLINE_DELAY_MS = 60 * 1000; // 停留超过 1 分钟才计入在线(由前端延迟首发实现)
const ONLINE_BEAT_MS = 30 * 1000; // 之后每 30s 心跳一次(后台标签被浏览器节流为 60s,服务端 90s 窗口判定)
let onlineStarted = false;
function isGuest(): boolean {
try {
return !JSON.parse(localStorage.getItem('it-tools-auth') ?? 'null');
}
catch {
return true;
}
}
export function startOnlineHeartbeat(): void {
if (onlineStarted) {
return;
}
onlineStarted = true;
const beat = () => {
try {
fetch('/relay/online', {
method: 'POST',
keepalive: true,
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ machine_id: getMachineId(), guest: isGuest() }),
}).catch(() => {});
}
catch {
// 统计失败绝不影响功能
}
};
setTimeout(() => {
beat();
setInterval(beat, ONLINE_BEAT_MS);
// 登录/登出立即同步:登录后该机器被移出在线统计
window.addEventListener('it-tools-auth-changed', beat);
}, ONLINE_DELAY_MS);
}