Skip to content
This repository was archived by the owner on May 13, 2024. It is now read-only.

Commit 9a8b6be

Browse files
Merge pull request #281 from utkarsha-deriv/utkarsha/DERA-453/websocket-connection-closed-due-to-timeout
2 parents 810e3b1 + a90f653 commit 9a8b6be

File tree

3 files changed

+32
-10
lines changed

3 files changed

+32
-10
lines changed

src/configs/websocket/index.ts

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,17 @@ export type TDerivApi = {
1515
authorize: (requestData: AuthorizeRequest) => Promise<AuthorizeResponse>;
1616
};
1717

18+
let attempts = 10;
19+
const RECONNECT_INTERVAL = attempts * 10000;
1820
const PING_INTERVAL = 12000;
1921

2022
export class ApiManager {
2123
private socket: WebSocket;
2224
private derivApi: TDerivApi;
2325
private pingInterval: NodeJS.Timer;
26+
private reconnectInterval: NodeJS.Timer;
27+
private is_websocket_connected: (connection_value) => boolean;
28+
private is_websocket_authorized: (connection_value) => boolean;
2429

2530
public static instance: ApiManager;
2631
public static getInstance() {
@@ -51,7 +56,9 @@ export class ApiManager {
5156
return this.derivApi.subscribe(request) as Observable<TSocketResponse<T>>;
5257
}
5358

54-
public authorize(token: string) {
59+
public authorize(token: string, setIsConnected, setIsAuthorized) {
60+
this.is_websocket_connected = setIsConnected;
61+
this.is_websocket_authorized = setIsAuthorized;
5562
return this.derivApi.authorize({ authorize: token });
5663
}
5764
public logout() {
@@ -62,14 +69,30 @@ export class ApiManager {
6269
if (this.pingInterval) {
6370
clearInterval(this.pingInterval);
6471
}
72+
if (this.reconnectInterval) {
73+
clearInterval(this.reconnectInterval);
74+
}
6575
this.socket.addEventListener('open', () => {
76+
this.is_websocket_connected?.(true);
6677
this.pingInterval = setInterval(() => {
6778
this.socket.send(JSON.stringify({ ping: 1 }));
6879
}, PING_INTERVAL);
6980
});
7081

7182
this.socket.addEventListener('close', () => {
83+
this.is_websocket_connected?.(false);
84+
this.is_websocket_authorized?.(false);
7285
clearInterval(this.pingInterval);
86+
this.socket = null;
87+
if (attempts > 0) {
88+
this.reconnectInterval = setTimeout(this.init.bind(this), RECONNECT_INTERVAL);
89+
attempts -= 1;
90+
} else {
91+
window.alert(
92+
'Sorry, the server is currently down. Please refresh the page or try again later',
93+
);
94+
clearInterval(this.reconnectInterval);
95+
}
7396
});
7497

7598
this.socket.addEventListener('error', () => {

src/contexts/auth/auth.provider.tsx

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ const AuthProvider = ({ children }: TAuthProviderProps) => {
2323
const [is_logged_in, setIsLoggedIn] = useState(false);
2424
const [is_authorized, setIsAuthorized] = useState(false);
2525
const [is_switching_account, setisSwitchingAccount] = useState(false);
26+
const [is_connected, setIsConnected] = useState(true);
2627

2728
const [loginAccounts, setLoginAccounts] = useSessionStorage<IUserLoginAccount[]>(
2829
LOGIN_ACCOUNTS_SESSION_STORAGE_KEY,
@@ -45,7 +46,11 @@ const AuthProvider = ({ children }: TAuthProviderProps) => {
4546

4647
const updateAuthorize = useCallback(async () => {
4748
if (currentLoginAccount.token) {
48-
const { authorize } = await apiManager.authorize(currentLoginAccount.token);
49+
const { authorize } = await apiManager.authorize(
50+
currentLoginAccount.token,
51+
setIsConnected,
52+
setIsAuthorized,
53+
);
4954
setIsAuthorized(true);
5055
setisSwitchingAccount(false);
5156
const { account_list, ...user } = authorize;
@@ -55,10 +60,10 @@ const AuthProvider = ({ children }: TAuthProviderProps) => {
5560
}, [currentLoginAccount.token, setUser, setUserAccounts]);
5661

5762
useEffect(() => {
58-
if (!is_authorized) {
63+
if (!is_authorized && is_connected) {
5964
updateAuthorize();
6065
}
61-
}, [is_authorized, updateAuthorize]);
66+
}, [is_authorized, is_connected, updateAuthorize]);
6267

6368
const updateLoginAccounts = useCallback(
6469
(loginAccounts: IUserLoginAccount[]) => {

src/contexts/playground/playground.provider.tsx

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
import React, { ReactNode, useEffect, useMemo, useState } from 'react';
2-
import apiManager from '@site/src/configs/websocket';
3-
import { getIsBrowser } from '@site/src/utils';
42
import { PlaygroundContext } from './playground.context';
53
import { TSocketResponseData } from '@site/src/configs/websocket/types';
64
import { TSocketEndpointNames } from '@site/src/configs/websocket/types';
@@ -9,10 +7,6 @@ type TPlaygroundProviderProps = {
97
children: ReactNode;
108
};
119

12-
if (getIsBrowser()) {
13-
apiManager.init();
14-
}
15-
1610
const PlaygroundProvider = <T extends TSocketEndpointNames>({
1711
children,
1812
}: TPlaygroundProviderProps) => {

0 commit comments

Comments
 (0)