UI protocol: update Insomnia requests collection
[e-mobility-charging-stations-simulator.git] / src / charging-station / ui-server / UIWebSocketServer.ts
index bb80c60a18c8f642b6483bcc5f961096348c9dcd..c067fb362a8c8b717312bcff35a049135aac2a23 100644 (file)
@@ -38,7 +38,6 @@ export default class UIWebSocketServer extends AbstractUIServer {
         );
         ws.close(WebSocketCloseEventStatusCode.CLOSE_PROTOCOL_ERROR);
       }
-      this.sockets.add(ws);
       this.registerProtocolVersionUIService(version);
       ws.on('message', (rawData) => {
         const request = this.validateRawDataRequest(rawData);
@@ -46,10 +45,11 @@ export default class UIWebSocketServer extends AbstractUIServer {
           ws.close(WebSocketCloseEventStatusCode.CLOSE_INVALID_PAYLOAD);
           return;
         }
-        const [messageId, procedureName, payload] = request as ProtocolRequest;
+        const [requestId] = request as ProtocolRequest;
+        this.responseHandlers.set(requestId, ws);
         this.uiServices
           .get(version)
-          .requestHandler(this.buildProtocolRequest(messageId, procedureName, payload))
+          .requestHandler(request)
           .catch(() => {
             /* Error caught by AbstractUIService */
           });
@@ -58,7 +58,6 @@ export default class UIWebSocketServer extends AbstractUIServer {
         logger.error(`${this.logPrefix(moduleName, 'start.ws.onerror')} WebSocket error:`, error);
       });
       ws.on('close', (code, reason) => {
-        this.sockets.delete(ws);
         logger.debug(
           `${this.logPrefix(
             moduleName,
@@ -94,8 +93,21 @@ export default class UIWebSocketServer extends AbstractUIServer {
   }
 
   public sendResponse(response: ProtocolResponse): void {
-    // TODO: send response only to the client that sent the request
-    this.broadcastToClients(JSON.stringify(response));
+    const responseId = response[0];
+    if (this.responseHandlers.has(responseId)) {
+      const ws = this.responseHandlers.get(responseId) as WebSocket;
+      if (ws?.readyState === WebSocket.OPEN) {
+        ws.send(JSON.stringify(response));
+      }
+      this.responseHandlers.delete(responseId);
+    } else {
+      logger.error(
+        `${this.logPrefix(
+          moduleName,
+          'sendResponse'
+        )} Response for unknown request id: ${responseId}`
+      );
+    }
   }
 
   public logPrefix(modName?: string, methodName?: string, prefixSuffix?: string): string {
@@ -115,18 +127,6 @@ export default class UIWebSocketServer extends AbstractUIServer {
     }
   }
 
-  private authenticate(req: IncomingMessage, next: (err?: Error) => void): void {
-    if (this.isBasicAuthEnabled() === true) {
-      if (this.isValidBasicAuth(req) === false) {
-        next(new Error('Unauthorized'));
-      } else {
-        next();
-      }
-    } else {
-      next();
-    }
-  }
-
   private validateRawDataRequest(rawData: RawData): ProtocolRequest | false {
     // logger.debug(
     //   `${this.logPrefix(