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); } }