From 815e34936fac0f94e9e2b5ecf7276798a97cd882 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Mon, 7 Dec 2020 12:11:18 +0100 Subject: [PATCH] Fix the reconnect logic. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/charging-station/ChargingStation.ts | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index b6c5fa2c..ada1c4b9 100644 --- a/src/charging-station/ChargingStation.ts +++ b/src/charging-station/ChargingStation.ts @@ -536,13 +536,16 @@ export default class ChargingStation { } } - _openWSConnection(options?: WebSocket.ClientOptions): void { + _openWSConnection(options?: WebSocket.ClientOptions, forceCloseOpened = false): void { if (Utils.isUndefined(options)) { options = {} as WebSocket.ClientOptions; } if (Utils.isUndefined(options.handshakeTimeout)) { options.handshakeTimeout = this._connectionTimeout; } + if (this._wsConnection?.readyState === WebSocket.OPEN && forceCloseOpened) { + this._wsConnection.close(); + } this._wsConnection = new WebSocket(this._wsConnectionUrl, 'ocpp' + Constants.OCPP_VERSION_16, options); logger.info(this._logPrefix() + ' Will communicate through URL ' + this._supervisionUrl); } @@ -600,6 +603,7 @@ export default class ChargingStation { await Utils.sleep(reconnectDelay); logger.error(this._logPrefix() + ' Socket: reconnecting try #' + this._autoReconnectRetryCount.toString()); this._openWSConnection({ handshakeTimeout: reconnectDelay - 100 }); + this._hasSocketRestarted = true; } else if (this._autoReconnectMaxRetries !== -1) { logger.error(`${this._logPrefix()} Socket: max retries reached (${this._autoReconnectRetryCount}) or retry disabled (${this._autoReconnectMaxRetries})`); } @@ -637,7 +641,6 @@ export default class ChargingStation { async onError(errorEvent): Promise { switch (errorEvent.code) { case 'ECONNREFUSED': - this._hasSocketRestarted = true; await this._reconnect(errorEvent); break; default: @@ -654,7 +657,6 @@ export default class ChargingStation { this._autoReconnectRetryCount = 0; break; default: // Abnormal close - this._hasSocketRestarted = true; await this._reconnect(closeEvent); break; } -- 2.34.1