Skip to content

Commit

Permalink
fix: socket timeout logic
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelTaylor3D committed Oct 5, 2023
1 parent 18b7978 commit 29aca01
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "core-registry-cadt-ui",
"version": "1.3.1",
"version": "1.3.2",
"private": true,
"author": "Chia Network Inc. <[email protected]>",
"homepage": "./",
Expand Down
34 changes: 28 additions & 6 deletions src/store/actions/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export const SOCKET_STATUS = keyMirror(
let socket;
let interval;
let reconnectInterval = 1000;
let reconnectAttempts = 0;

const notifyRefresh = _.debounce(dispatch => {
NotificationManager.info(
Expand All @@ -33,7 +34,7 @@ const notifyRefresh = _.debounce(dispatch => {
() => dispatch(refreshApp(true)),
true,
);
}, 100);
}, 1000);

const initListenersForEachMessageType = dispatch => {
Object.keys(messageTypes).forEach(key => {
Expand Down Expand Up @@ -84,20 +85,35 @@ export const emitAction = actionCreator => {
};

const reconnectSocket = _.debounce(dispatch => {
if (!socket || (socket && !socket.connected)) {
if (reconnectAttempts <= 10 && (!socket || !socket.connected)) {
console.log(
'Attempting to reconnect to socket server...',
reconnectAttempts,
);
dispatch(initiateSocket());
setTimeout(() => {
if (!socket || (socket && !socket.connected)) {
if (!socket || !socket.connected) {
reconnectSocket(dispatch);
}
reconnectInterval = reconnectInterval * 2;
reconnectInterval *= 2;
}, reconnectInterval);
} else if (reconnectAttempts > 10) {
console.log("Couldn't connect to socket server. Max attempts reached.");
dispatch(setSocketStatus(SOCKET_STATUS.OFFLINE));

clearInterval(interval);
}
}, 100);
}, 1000);

export const initiateSocket = remoteHost => {
disconnectSocket();

if (reconnectAttempts > 10) {
return;
}

reconnectAttempts++;

const WS_HOST = `${remoteHost || constants.API_HOST}/ws`;
const transports = ['websocket'];

Expand Down Expand Up @@ -158,16 +174,22 @@ export const initiateSocket = remoteHost => {
console.log('Attempting to connect to socket_id: ', socket.id);
console.log('### Socket Connected ###');
dispatch(setSocketStatus(SOCKET_STATUS.CONNECTED));
reconnectAttempts = 0;
socket.emit('authentication');
});

initListenersForEachMessageType(dispatch);

interval = setInterval(() => {
if (!socket || (socket && !socket.connected)) {
if (reconnectAttempts <= 10 && (!socket || !socket.connected)) {
dispatch(setSocketStatus(SOCKET_STATUS.OFFLINE));
clearInterval(interval);
reconnectSocket(dispatch);
} else if (reconnectAttempts > 10) {
console.log("Couldn't connect to socket server. Max attempts reached.");
dispatch(setSocketStatus(SOCKET_STATUS.OFFLINE));
clearInterval(interval);
disconnectSocket();
}
}, 5000);
};
Expand Down

0 comments on commit 29aca01

Please sign in to comment.