refactor: enhance session management and room joining logic in WebSocket handling
All checks were successful
Build and Push Docker Image / docker (push) Successful in 9s
All checks were successful
Build and Push Docker Image / docker (push) Successful in 9s
This commit is contained in:
@@ -4,6 +4,8 @@ let ws;
|
||||
let reconnectAttempts = 0;
|
||||
let reconnectTimer = null;
|
||||
const outbox = [];
|
||||
let sessionId = localStorage.getItem('sessionId') || null;
|
||||
let lastRoomId = localStorage.getItem('lastRoomId') || null;
|
||||
|
||||
export function wsIsOpen() { return ws && ws.readyState === WebSocket.OPEN; }
|
||||
export function sendMsg(obj) { if (wsIsOpen()) ws.send(JSON.stringify(obj)); else outbox.push(obj); }
|
||||
@@ -20,6 +22,10 @@ export function connectWS(onMessage) {
|
||||
ws = new WebSocket(url);
|
||||
ws.addEventListener('open', () => {
|
||||
reconnectAttempts = 0;
|
||||
// Try to resume session immediately on (re)connect
|
||||
if (sessionId) {
|
||||
try { ws.send(JSON.stringify({ type: 'resume', sessionId })); } catch {}
|
||||
}
|
||||
setTimeout(() => { while (outbox.length && wsIsOpen()) { try { ws.send(JSON.stringify(outbox.shift())); } catch { break; } } }, 100);
|
||||
});
|
||||
ws.addEventListener('message', (ev) => onMessage(ev));
|
||||
@@ -33,3 +39,15 @@ window.addEventListener('online', () => {
|
||||
// Kick off a reconnect by calling connectWS from app main again
|
||||
}
|
||||
});
|
||||
|
||||
// Helpers to update cached ids from other modules
|
||||
export function cacheSessionId(id) {
|
||||
if (!id) return;
|
||||
sessionId = id;
|
||||
try { localStorage.setItem('sessionId', id); } catch {}
|
||||
}
|
||||
export function cacheLastRoomId(id) {
|
||||
if (!id) return;
|
||||
lastRoomId = id;
|
||||
try { localStorage.setItem('lastRoomId', id); } catch {}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user