✨ auto-reconnect the exam room after a dropped socket
Browse files at this commit1edb4f31d2328e06dc60a3f9944c2c684059a457Parent: bc3b5ce- feat
- ✨ auto-reconnect the exam room after a dropped socket
1 changed file +50 -2
src/components/exams/exam-room-ws.tsxmodified+50 -2
| @@ -94,26 +94,72 @@ export function ExamRoomWS(props: { exam: Exam }) { | |||
| 94 | }; | 94 | }; |
| 95 | 95 | ||
| 96 | let ws: WebSocket | null = null; | 96 | let ws: WebSocket | null = null; |
| 97 | + // Auto-reconnect a socket that drops mid-sitting: a transient network blip | ||
| 98 | + // shouldn't freeze the live countdown/state at "disconnected". Rejoining also | ||
| 99 | + // clears the backend's `left_at` (which any socket close stamps) — so on an | ||
| 100 | + // `allow_rejoin` exam a blip recovers cleanly; on a locked exam the rejoin | ||
| 101 | + // 409s, which is the intended lockout. Backoff caps the churn; the counter | ||
| 102 | + // resets on a successful open. | ||
| 103 | + let reconnectTimer: ReturnType<typeof setTimeout> | null = null; | ||
| 104 | + let reconnectAttempts = 0; | ||
| 105 | + let closedByUs = false; | ||
| 106 | + const MAX_RECONNECT = 6; | ||
| 107 | + | ||
| 108 | + const clearReconnect = () => { | ||
| 109 | + if (reconnectTimer) { | ||
| 110 | + clearTimeout(reconnectTimer); | ||
| 111 | + reconnectTimer = null; | ||
| 112 | + } | ||
| 113 | + }; | ||
| 97 | 114 | ||
| 98 | const wsUrl = () => { | 115 | const wsUrl = () => { |
| 99 | const proto = window.location.protocol === "https:" ? "wss:" : "ws:"; | 116 | const proto = window.location.protocol === "https:" ? "wss:" : "ws:"; |
| 100 | return `${proto}//${window.location.host}/api/exams/${encodeURIComponent(props.exam.id)}/attempt/ws`; | 117 | return `${proto}//${window.location.host}/api/exams/${encodeURIComponent(props.exam.id)}/attempt/ws`; |
| 101 | }; | 118 | }; |
| 102 | 119 | ||
| 120 | + // Retry only while the sitting is genuinely live and we didn't close on | ||
| 121 | + // purpose: a submitted/expired attempt (or a client-side expiry) is terminal, | ||
| 122 | + // and reconnecting there would only 409. | ||
| 123 | + const scheduleReconnect = () => { | ||
| 124 | + if (closedByUs || reconnectTimer) return; | ||
| 125 | + if (!roomOpen()) return; | ||
| 126 | + if (attempt()?.status !== "in_progress" || attemptStatus() !== "in_progress") return; | ||
| 127 | + if (reconnectAttempts >= MAX_RECONNECT) return; | ||
| 128 | + const delay = Math.min(1000 * 2 ** reconnectAttempts, 15000); | ||
| 129 | + reconnectAttempts += 1; | ||
| 130 | + reconnectTimer = setTimeout(() => { | ||
| 131 | + reconnectTimer = null; | ||
| 132 | + connectWs(); | ||
| 133 | + }, delay); | ||
| 134 | + }; | ||
| 135 | + | ||
| 103 | const connectWs = () => { | 136 | const connectWs = () => { |
| 104 | - if (ws) ws.close(); | 137 | + clearReconnect(); |
| 138 | + // Detach the old socket's handlers before closing it: an intentional close | ||
| 139 | + // must not look like a drop and trigger a reconnect. | ||
| 140 | + if (ws) { | ||
| 141 | + ws.onclose = null; | ||
| 142 | + ws.onerror = null; | ||
| 143 | + ws.onmessage = null; | ||
| 144 | + ws.close(); | ||
| 145 | + } | ||
| 105 | setWsState("connecting"); | 146 | setWsState("connecting"); |
| 106 | const socket = new WebSocket(wsUrl()); | 147 | const socket = new WebSocket(wsUrl()); |
| 107 | ws = socket; | 148 | ws = socket; |
| 108 | 149 | ||
| 109 | - socket.onopen = () => setWsState("connected"); | 150 | + socket.onopen = () => { |
| 151 | + reconnectAttempts = 0; | ||
| 152 | + setWsState("connected"); | ||
| 153 | + }; | ||
| 110 | socket.onclose = () => { | 154 | socket.onclose = () => { |
| 111 | setWsState("disconnected"); | 155 | setWsState("disconnected"); |
| 112 | ws = null; | 156 | ws = null; |
| 157 | + scheduleReconnect(); | ||
| 113 | }; | 158 | }; |
| 114 | socket.onerror = () => { | 159 | socket.onerror = () => { |
| 115 | setWsState("disconnected"); | 160 | setWsState("disconnected"); |
| 116 | ws = null; | 161 | ws = null; |
| 162 | + scheduleReconnect(); | ||
| 117 | }; | 163 | }; |
| 118 | 164 | ||
| 119 | socket.onmessage = (event) => { | 165 | socket.onmessage = (event) => { |
| @@ -306,6 +352,8 @@ export function ExamRoomWS(props: { exam: Exam }) { | |||
| 306 | const goNext = () => setActiveQuestionIndex((index) => Math.min(questions().length - 1, index + 1)); | 352 | const goNext = () => setActiveQuestionIndex((index) => Math.min(questions().length - 1, index + 1)); |
| 307 | 353 | ||
| 308 | onCleanup(() => { | 354 | onCleanup(() => { |
| 355 | + closedByUs = true; | ||
| 356 | + clearReconnect(); | ||
| 309 | if (ws) ws.close(); | 357 | if (ws) ws.close(); |
| 310 | }); | 358 | }); |
| 311 | 359 | ||