Add helper in LRU cache to get cache key
[e-mobility-charging-stations-simulator.git] / src / charging-station / ui-server / UIWebSocketServer.ts
index 98dd52b2489fdeb1dc82670ed40339d2934893a5..c67da00e1f6c8643e0f35032ff759d6bfe0cb0f0 100644 (file)
@@ -1,26 +1,22 @@
 import { Protocol, ProtocolVersion } from '../../types/UIProtocol';
-import WebSocket, { OPEN, Server, ServerOptions } from 'ws';
 
-import AbstractUIService from './ui-services/AbstractUIService';
+import { AbstractUIServer } from './AbstractUIServer';
 import Configuration from '../../utils/Configuration';
 import { IncomingMessage } from 'http';
+import { ServerOptions } from '../../types/ConfigurationData';
 import UIServiceFactory from './ui-services/UIServiceFactory';
 import Utils from '../../utils/Utils';
+import WebSocket from 'ws';
 import logger from '../../utils/Logger';
 
-export default class UIWebSocketServer extends Server {
-  public readonly chargingStations: Set<string>;
-  public readonly uiServices: Map<ProtocolVersion, AbstractUIService>;
-
-  public constructor(options?: ServerOptions, callback?: () => void) {
-    // Create the WebSocket Server
-    super(options ?? Configuration.getUIServer().options, callback);
-    this.chargingStations = new Set<string>();
-    this.uiServices = new Map<ProtocolVersion, AbstractUIService>();
+export default class UIWebSocketServer extends AbstractUIServer {
+  public constructor(options?: ServerOptions) {
+    super();
+    this.server = new WebSocket.Server(options ?? Configuration.getUIServer().options);
   }
 
   public start(): void {
-    this.on('connection', (socket: WebSocket, request: IncomingMessage): 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
@@ -44,7 +40,7 @@ export default class UIWebSocketServer extends Server {
   }
 
   public stop(): void {
-    this.close();
+    this.server.close();
   }
 
   public sendResponse(message: string): void {
@@ -56,8 +52,8 @@ export default class UIWebSocketServer extends Server {
   }
 
   private broadcastToClients(message: string): void {
-    for (const client of this.clients) {
-      if (client?.readyState === OPEN) {
+    for (const client of (this.server as WebSocket.Server).clients) {
+      if (client?.readyState === WebSocket.OPEN) {
         client.send(message);
       }
     }