diff --git a/package.json b/package.json index 9e576021..433d0591 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "cadt-ui", - "version": "1.2.7", + "version": "1.2.8", "private": true, "author": "Chia Network Inc. ", "homepage": "./", diff --git a/src/store/actions/socket.js b/src/store/actions/socket.js index 51ed4d05..a2785e11 100644 --- a/src/store/actions/socket.js +++ b/src/store/actions/socket.js @@ -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( @@ -33,7 +34,7 @@ const notifyRefresh = _.debounce(dispatch => { () => dispatch(refreshApp(true)), true, ); -}, 100); +}, 1000); const initListenersForEachMessageType = dispatch => { Object.keys(messageTypes).forEach(key => { @@ -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']; @@ -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); };