Improve SSE status and event auth handling

This commit is contained in:
Brandon Zhang
2026-03-27 17:55:28 +08:00
parent f437f6939c
commit 65b29bcf03
6 changed files with 77 additions and 22 deletions

View File

@@ -6,7 +6,7 @@
*/
import { state } from './state.js';
import { api } from './api.js';
import { api, getStoredToken } from './api.js';
let _es = null;
let _reconnectTimer = null;
@@ -19,7 +19,12 @@ export function connectSSE() {
}
function _connect() {
_es = new EventSource('/api/events');
const token = getStoredToken();
const url = token
? `/api/events?access_token=${encodeURIComponent(token)}`
: '/api/events';
_es = new EventSource(url);
_es.onopen = () => {
console.debug('[SSE] connected');
@@ -43,7 +48,6 @@ function _connect() {
_es.onerror = () => {
console.warn('[SSE] connection lost reconnecting in', RECONNECT_DELAY_MS, 'ms');
state.set('serverOnline', false);
_reconnecting = true;
state.set('sseReconnecting', true);
_es.close();