Changes before error encountered
Co-authored-by: pdf114514 <57948770+pdf114514@users.noreply.github.com> Agent-Logs-Url: https://github.com/pdf114514/CarReservation/sessions/a42d4e36-a3cf-4ff7-b1cb-f076e601b1b8
This commit is contained in:
@@ -5,8 +5,9 @@ const WS_BASE = (() => {
|
||||
const loc = window.location;
|
||||
const proto = loc.protocol === 'https:' ? 'wss:' : 'ws:';
|
||||
// Use the same origin as the page; in development the Vite dev server
|
||||
// proxies /ws to the backend, and in production nginx does the same.
|
||||
return `${proto}//${loc.host}/ws`;
|
||||
// proxies /api/ws to the backend, and in production nginx does the same
|
||||
// via the existing location /api/ block (no extra nginx config needed).
|
||||
return `${proto}//${loc.host}/api/ws`;
|
||||
})();
|
||||
|
||||
const RECONNECT_DELAYS = [1000, 2000, 4000, 8000, 15000, 30000]; // ms
|
||||
@@ -71,11 +72,30 @@ export default function useWebSocket(onMessage) {
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
// Reset on each mount so that React StrictMode's simulated unmount/remount
|
||||
// does not leave unmountedRef permanently true and block reconnection.
|
||||
unmountedRef.current = false;
|
||||
connect();
|
||||
return () => {
|
||||
unmountedRef.current = true;
|
||||
clearTimeout(retryTimerRef.current);
|
||||
wsRef.current?.close();
|
||||
const ws = wsRef.current;
|
||||
if (ws) {
|
||||
// Null out all handlers first to prevent any reconnect attempts.
|
||||
ws.onopen = null;
|
||||
ws.onclose = null;
|
||||
ws.onerror = null;
|
||||
ws.onmessage = null;
|
||||
if (ws.readyState === WebSocket.CONNECTING) {
|
||||
// Closing a CONNECTING socket triggers the browser warning
|
||||
// "WebSocket is closed before the connection is established".
|
||||
// Instead, schedule a close as soon as it opens so the server
|
||||
// isn't left with a permanently idle connection.
|
||||
ws.onopen = () => ws.close();
|
||||
} else if (ws.readyState === WebSocket.OPEN) {
|
||||
ws.close();
|
||||
}
|
||||
}
|
||||
};
|
||||
}, [connect]);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user