Avoid accessing the WS on close event
MODIFY
src/server/ocpp/json/JsonCentralSystemServer.ts
src/server/ocpp/json/JsonCentralSystemServer.ts
@@ -66,7 +66,7 @@ export default class JsonCentralSystemServer extends CentralSystemServer {
const reason = Utils.convertBufferArrayToString(message);
// Close
this.isDebug() && Logging.logConsoleDebug(`WS Closed received for '${ws.wsWrapper.url as string}'`);
- await this.closeWebSocket(ws.wsWrapper, code, reason);
+ await this.closeWebSocket(ws.wsWrapper, code, reason, true);
},
ping: async (ws: WebSocket, message: ArrayBuffer) => {
// Convert
@@ -459,9 +459,9 @@ export default class JsonCentralSystemServer extends CentralSystemServer {
}
}
- private async closeWebSocket(wsWrapper: WSWrapper, code: WebSocketCloseEventStatusCode, reason: string): Promise<void> {
+ private async closeWebSocket(wsWrapper: WSWrapper, code: WebSocketCloseEventStatusCode, reason: string, fromCloseEvent = false): Promise<void> {
// Keep status
- wsWrapper.close(code, reason);
+ wsWrapper.close(code, reason, fromCloseEvent);
// Check Json connection
if (wsWrapper.jsonWSConnection) {
this.removeJsonWSConnection(wsWrapper.jsonWSConnection);

MODIFY
src/server/ocpp/json/WSWrapper.ts
src/server/ocpp/json/WSWrapper.ts
@@ -42,13 +42,16 @@ export default class WSWrapper {
return this.ws.getBufferedAmount();
}
- public close(code: number, shortMessage: RecognizedString): void {
+ public close(code: number, shortMessage: RecognizedString, fromCloseEvent = false): void {
if (!this.closed) {
this.closed = true;
try {
+ // Already clsoed?
+ if (!fromCloseEvent) {
this.ws.end(code, shortMessage);
+ }
} catch (error) {
- console.log(`Error closing ${error?.message as string}`);
+ console.log(`Error closing WS: ${error?.message as string}`);
}
}
}

Avoid accessing the WS on close event