fix: reduce emby load and stabilize strm scan

This commit is contained in:
ShukeBta
2026-06-11 00:18:51 +08:00
parent 859d6e9d24
commit 542e85a067
22 changed files with 1040 additions and 91 deletions
+14 -3
View File
@@ -2,9 +2,12 @@ import { useEffect, useRef } from 'react'
import { useAuthStore } from '../stores/auth'
const MAX_RECONNECT_ATTEMPTS = 5
// useWebSocket opens a single connection to /api/ws and dispatches every
// message to the supplied handler. Auto-reconnects with a 3 s back-off
// while the auth token is present.
// message to the supplied handler. Auto-reconnects with back-off while the
// auth token is present, but stops after repeated failures so an expired token
// cannot create an endless /api/ws 401 loop.
export function useWebSocket(onEvent: (topic: string, payload: unknown) => void) {
const ref = useRef<WebSocket | null>(null)
const token = useAuthStore((s) => s.token)
@@ -13,12 +16,18 @@ export function useWebSocket(onEvent: (topic: string, payload: unknown) => void)
if (!token) return
let closed = false
let timer: number | undefined
let reconnectAttempts = 0
const open = () => {
if (closed) return
if (reconnectAttempts >= MAX_RECONNECT_ATTEMPTS) return
const proto = window.location.protocol === 'https:' ? 'wss:' : 'ws:'
const url = `${proto}//${window.location.host}/api/ws?token=${encodeURIComponent(token)}`
const ws = new WebSocket(url)
ref.current = ws
ws.onopen = () => {
reconnectAttempts = 0
}
ws.onmessage = (ev) => {
try {
const msg = JSON.parse(ev.data)
@@ -31,7 +40,9 @@ export function useWebSocket(onEvent: (topic: string, payload: unknown) => void)
}
ws.onclose = () => {
if (closed) return
timer = window.setTimeout(open, 3_000)
reconnectAttempts += 1
const delay = Math.min(3_000 * reconnectAttempts, 30_000)
timer = window.setTimeout(open, delay)
}
}