From 6e005ce758f125d7def0ebf64b3b12419b3a98e7 Mon Sep 17 00:00:00 2001 From: Salvo Miosi Date: Sat, 28 Sep 2024 17:14:38 +0200 Subject: [PATCH] add 1s timeout before reconnect --- src/Model/UseBangConnection.ts | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/Model/UseBangConnection.ts b/src/Model/UseBangConnection.ts index 9507ea84..21c10a2c 100644 --- a/src/Model/UseBangConnection.ts +++ b/src/Model/UseBangConnection.ts @@ -77,7 +77,7 @@ export default function useBangConnection() { const connection = useWebSocket(Env.bangServerUrl); - const reconnecting = useRef(false); + const reconnecting = useRef(); const initial = useEvent(() => { if (settings.sessionId) { @@ -86,7 +86,9 @@ export default function useBangConnection() { }); const connected = useEvent(async () => { - reconnecting.current = false; + clearTimeout(reconnecting.current); + reconnecting.current = undefined; + connection.sendMessage({ connect: { username: settings.username || '', @@ -106,8 +108,7 @@ export default function useBangConnection() { sceneDispatch({ setError: { type: 'server', code, message: 'ERROR_DISCONNECTED_FROM_SERVER' }}); if (!reconnecting.current && code !== null && code !== 1000) { sceneDispatch({ gotoLoading: 'RECONNECTING' }); - reconnecting.current = true; - connection.connect(); + reconnecting.current = setTimeout(() => connection.connect(), 1000); } } }); @@ -172,8 +173,9 @@ export default function useBangConnection() { const handleConnect = useEvent(() => { if (connection.connectionState.state !== 'connected') { + clearTimeout(reconnecting.current); + reconnecting.current = undefined; connection.connect(); - reconnecting.current = false; sceneDispatch({ gotoLoading: 'LOADING' }); } });