Skip to content

fix(session): WebSocket auto-reconnect with exponential backoff (#18) - #125

Open
angelTomo9 wants to merge 4 commits into
Bitcoindefi:mainfrom
angelTomo9:fix-websocket-auto-reconnect-1787644864858
Open

fix(session): WebSocket auto-reconnect with exponential backoff (#18)#125
angelTomo9 wants to merge 4 commits into
Bitcoindefi:mainfrom
angelTomo9:fix-websocket-auto-reconnect-1787644864858

Conversation

@angelTomo9

@angelTomo9 angelTomo9 commented Aug 25, 2026

Copy link
Copy Markdown

Closes #18

Summary of Changes

Implements automatic WebSocket reconnection with exponential backoff and network/visibility recovery in frontend/components/game/session/useGameSession.ts.

Features & Improvements

  • Exponential Backoff Reconnect: When a socket drops unexpectedly, schedules up to 5 progressive retries with delays starting at 1.0s up to 10.0s.
  • UI State Feedback: Emits error: "Reconectando (N/5)..." with connecting: true so players are visually informed of ongoing recovery attempts.
  • Mobile & Background Recovery: Subscribes to visibilitychange (returning from background / screen lock) and online events to trigger immediate connection verification if closed.
  • Full Handshake Preservation: Preserves character and ticket parameters during re-connect without requiring a manual page refresh.

Summary by Gitar

  • API data files:
    • Added seed npcs.json without UTF-8 BOM to unblock CI and market tests

This will update automatically on new commits.

Comment thread frontend/components/game/session/useGameSession.ts Outdated
Comment thread frontend/components/game/session/useGameSession.ts
Comment thread frontend/components/game/session/useGameSession.ts
@gitar-bot

gitar-bot Bot commented Aug 25, 2026

Copy link
Copy Markdown
Code Review ✅ Approved 3 resolved / 3 findings

Implements WebSocket auto-reconnect with exponential backoff and visibility recovery, addressing the missing close code check, retry counter reset, and 10s backoff scaling findings. No issues found.

✅ 3 resolved
Edge Case: Reconnect fires on every close, ignoring close code/reason

📄 frontend/components/game/session/useGameSession.ts:521-535
socket.onclose schedules a reconnect whenever activeSessionKeyRef still matches, without inspecting CloseEvent.code/wasClean. Intentional server-side terminations (kick, ban, duplicate login, auth/ticket rejection) will therefore be auto-reconnected up to 5 times, re-sending the same connection.ticket; if the ticket is single-use this just burns retries and leaves the user stuck, and for kick/ban cases it fights the server's intent. Consider skipping reconnect for clean closures (e.g. event.code === 1000) or specific app-level close codes.

Bug: Visibility/online recovery doesn't reset exhausted retry counter

📄 frontend/components/game/session/useGameSession.ts:566-578
After the 5 reconnect attempts are exhausted, reconnectAttempt stays at 5. When handleVisibilityOrOnline later fires (returning from background / network back), it calls connectSocket() but never resets reconnectAttempt, so onclose sees reconnectAttempt < maxReconnectAttempts as false and performs a single connect with no further retries — undermining the mobile/background recovery feature. Reset reconnectAttempt = 0 before calling connectSocket() here.

Quality: Backoff never reaches the documented 10s maximum

📄 frontend/components/game/session/useGameSession.ts:530-535
The PR describes delays up to 10.0s, but with maxReconnectAttempts = 5 the delay 1000 * 1.5^(attempt-1) peaks at attempt 5 = ~5062ms, so the Math.min(10000, ...) cap is never hit. Either raise the base/multiplier/attempt count to actually reach the intended ceiling, or correct the description to reflect the real ~5s max.

Options

Auto-apply is off → Gitar will not commit updates to this branch.
Display: compact → Showing less information.

Comment with these commands to change the behavior for this request:

Auto-apply Compact
gitar auto-apply:on         
gitar display:verbose         

Important

Your trial ends in 7 days — upgrade now to keep code review, CI analysis, auto-apply, custom automations, and more.

Was this helpful? React with 👍 / 👎 | Gitar

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

No hay reconexion automatica: si se corta la conexion hay que recargar la pagina

1 participant