From 7ed9ca54f28c86e71a3ceea759fec81acfc9373e Mon Sep 17 00:00:00 2001 From: enahum Date: Thu, 16 Feb 2017 11:44:14 -0300 Subject: [PATCH] PLT-5543 Fix WebSocket keeps trying to reconnect (#261) * PLT-5543 Fix WebSocket keeps trying to reconnect when the session has expired * force connection when initializing the websocket --- service/actions/websocket.js | 4 ++-- service/client/websocket_client.js | 17 +++++++++++++---- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/service/actions/websocket.js b/service/actions/websocket.js index 2a9d28033..d118db7f6 100644 --- a/service/actions/websocket.js +++ b/service/actions/websocket.js @@ -60,13 +60,13 @@ export function init(siteUrl, token, optionalWebSocket) { websocketClient.setReconnectCallback(handleReconnect); websocketClient.setCloseCallback(handleClose); websocketClient.setConnectingCallback(handleConnecting); - return websocketClient.initialize(connUrl, authToken, dispatch, getState, optionalWebSocket); + return websocketClient.initialize(true, connUrl, authToken, dispatch, getState, optionalWebSocket); }; } export function close() { return async (dispatch, getState) => { - websocketClient.close(); + websocketClient.close(true); if (dispatch) { dispatch({type: GeneralTypes.WEBSOCKET_FAILURE, error: 'Closed'}, getState); } diff --git a/service/client/websocket_client.js b/service/client/websocket_client.js index a903216b9..6503e630a 100644 --- a/service/client/websocket_client.js +++ b/service/client/websocket_client.js @@ -22,9 +22,14 @@ class WebSocketClient { this.connectingCallback = null; this.dispatch = null; this.getState = null; + this.stop = false; } - initialize(connectionUrl = this.connectionUrl, token, dispatch, getState, webSocketConnector = WebSocket) { + initialize(forceConnection = true, connectionUrl = this.connectionUrl, token, dispatch, getState, webSocketConnector = WebSocket) { + if (forceConnection) { + this.stop = false; + } + return new Promise((resolve, reject) => { if (this.conn) { resolve(); @@ -104,7 +109,10 @@ class WebSocketClient { setTimeout( () => { - this.initialize(connectionUrl, token, dispatch, getState, webSocketConnector); + if (this.stop) { + return; + } + this.initialize(false, connectionUrl, token, dispatch, getState, webSocketConnector); }, retryTime ); @@ -158,7 +166,8 @@ class WebSocketClient { this.closeCallback = callback; } - close() { + close(stop = false) { + this.stop = stop; this.connectFailCount = 0; this.sequence = 1; if (this.conn && this.conn.readyState === Socket.OPEN) { @@ -180,7 +189,7 @@ class WebSocketClient { this.conn.send(JSON.stringify(msg)); } else if (!this.conn || this.conn.readyState === Socket.CLOSED) { this.conn = null; - this.initialize(this.connectionUrl, this.token, this.dispatch, this.getState); + this.initialize(false, this.connectionUrl, this.token, this.dispatch, this.getState); } }