Apply dependencies update
[e-mobility-charging-stations-simulator.git] / src / charging-station / ui-server / UIWebSocketServer.ts
index 66980f610f08e86a4f111ded7db26a9bab7ec41e..85e2d669cf272e280ec1f369cdd65e5d62c1163d 100644 (file)
@@ -1,14 +1,15 @@
-import { IncomingMessage } from 'http';
+import type { IncomingMessage } from 'http';
 
 import WebSocket from 'ws';
 
-import { ServerOptions } from '../../types/ConfigurationData';
-import { Protocol, ProtocolVersion } from '../../types/UIProtocol';
+import type { ServerOptions } from '../../types/ConfigurationData';
+import { WebSocketCloseEventStatusCode } from '../../types/WebSocket';
 import Configuration from '../../utils/Configuration';
 import logger from '../../utils/Logger';
 import Utils from '../../utils/Utils';
 import { AbstractUIServer } from './AbstractUIServer';
 import UIServiceFactory from './ui-services/UIServiceFactory';
+import { UIServiceUtils } from './ui-services/UIServiceUtils';
 
 const moduleName = 'UIWebSocketServer';
 
@@ -20,26 +21,26 @@ export default class UIWebSocketServer extends AbstractUIServer {
 
   public start(): void {
     this.server.on('connection', (socket: WebSocket, request: IncomingMessage): void => {
-      const protocolIndex = socket.protocol.indexOf(Protocol.UI);
-      const version = socket.protocol.substring(
-        protocolIndex + Protocol.UI.length
-      ) as ProtocolVersion;
+      const [protocol, version] = UIServiceUtils.getProtocolAndVersion(socket.protocol);
+      if (UIServiceUtils.isProtocolAndVersionSupported(protocol, version) === false) {
+        logger.error(
+          `${this.logPrefix(
+            moduleName,
+            'start.server.onconnection'
+          )} Unsupported UI protocol version: '${protocol}${version}'`
+        );
+        socket.close(WebSocketCloseEventStatusCode.CLOSE_PROTOCOL_ERROR);
+      }
       if (!this.uiServices.has(version)) {
         this.uiServices.set(version, UIServiceFactory.getUIServiceImplementation(version, this));
       }
       // FIXME: check connection validity
-      socket.on('message', (messageData) => {
+      socket.on('message', (rawData) => {
         this.uiServices
           .get(version)
-          .requestHandler(messageData)
-          .catch((error) => {
-            logger.error(
-              `${this.logPrefix(
-                moduleName,
-                'start.socket.onmessage'
-              )} Error while handling message:`,
-              error
-            );
+          .requestHandler(rawData)
+          .catch(() => {
+            /* Error caught by AbstractUIService */
           });
       });
       socket.on('error', (error) => {
@@ -52,7 +53,7 @@ export default class UIWebSocketServer extends AbstractUIServer {
   }
 
   public stop(): void {
-    this.server.close();
+    this.chargingStations.clear();
   }
 
   public sendRequest(request: string): void {
@@ -60,6 +61,7 @@ export default class UIWebSocketServer extends AbstractUIServer {
   }
 
   public sendResponse(response: string): void {
+    // TODO: send response only to the client that sent the request
     this.broadcastToClients(response);
   }