From: Sébastien Savalle <> Date: Thu, 16 Jun 2022 12:48:54 +0000 (+0200) Subject: Fix websocket reconnection X-Git-Tag: v1.1.63~4^2 X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=a1d6ac7f07c2753f539d50d52bd97be5454c020e;p=e-mobility-charging-stations-simulator.git Fix websocket reconnection --- diff --git a/src/charging-station/ChargingStation.ts b/src/charging-station/ChargingStation.ts index 772389de..82aaa25c 100644 --- a/src/charging-station/ChargingStation.ts +++ b/src/charging-station/ChargingStation.ts @@ -479,27 +479,6 @@ export default class ChargingStation { this.performanceStatistics.start(); } this.openWSConnection(); - // Handle WebSocket message - this.wsConnection.on( - 'message', - this.onMessage.bind(this) as (this: WebSocket, data: RawData, isBinary: boolean) => void - ); - // Handle WebSocket error - this.wsConnection.on( - 'error', - this.onError.bind(this) as (this: WebSocket, error: Error) => void - ); - // Handle WebSocket close - this.wsConnection.on( - 'close', - this.onClose.bind(this) as (this: WebSocket, code: number, reason: Buffer) => void - ); - // Handle WebSocket open - this.wsConnection.on('open', this.onOpen.bind(this) as (this: WebSocket) => void); - // Handle WebSocket ping - this.wsConnection.on('ping', this.onPing.bind(this) as (this: WebSocket, data: Buffer) => void); - // Handle WebSocket pong - this.wsConnection.on('pong', this.onPong.bind(this) as (this: WebSocket, data: Buffer) => void); // Monitor charging station template file this.templateFileWatcher = FileUtils.watchJsonFile( this.logPrefix(), @@ -1496,6 +1475,7 @@ export default class ChargingStation { } private onError(error: WSError): void { + this.wsConnection.close(); logger.error(this.logPrefix() + ' WebSocket error: %j', error); } @@ -1907,10 +1887,34 @@ export default class ChargingStation { this.handleUnsupportedVersion(this.getOcppVersion()); break; } - this.wsConnection = new WebSocket(this.wsConnectionUrl, protocol, options); + logger.info( this.logPrefix() + ' Open OCPP connection to URL ' + this.wsConnectionUrl.toString() ); + + this.wsConnection = new WebSocket(this.wsConnectionUrl, protocol, options); + + // Handle WebSocket message + this.wsConnection.on( + 'message', + this.onMessage.bind(this) as (this: WebSocket, data: RawData, isBinary: boolean) => void + ); + // Handle WebSocket error + this.wsConnection.on( + 'error', + this.onError.bind(this) as (this: WebSocket, error: Error) => void + ); + // Handle WebSocket close + this.wsConnection.on( + 'close', + this.onClose.bind(this) as (this: WebSocket, code: number, reason: Buffer) => void + ); + // Handle WebSocket open + this.wsConnection.on('open', this.onOpen.bind(this) as (this: WebSocket) => void); + // Handle WebSocket ping + this.wsConnection.on('ping', this.onPing.bind(this) as (this: WebSocket, data: Buffer) => void); + // Handle WebSocket pong + this.wsConnection.on('pong', this.onPong.bind(this) as (this: WebSocket, data: Buffer) => void); } private closeWSConnection(): void {