Skip to content

Commit

Permalink
add 1s timeout before reconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
salvoilmiosi committed Sep 28, 2024
1 parent 3bacf70 commit 6e005ce
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/Model/UseBangConnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export default function useBangConnection() {

const connection = useWebSocket<ServerMessage, ClientMessage>(Env.bangServerUrl);

const reconnecting = useRef(false);
const reconnecting = useRef<number>();

const initial = useEvent(() => {
if (settings.sessionId) {
Expand All @@ -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 || '',
Expand All @@ -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);
}
}
});
Expand Down Expand Up @@ -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' });
}
});
Expand Down

0 comments on commit 6e005ce

Please sign in to comment.