X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2Fui-server%2FUIWebSocketServer.ts;h=c067fb362a8c8b717312bcff35a049135aac2a23;hb=cfa257f5b0065aa6e4a7263cf21fc8305466303a;hp=f789d3963e881762b58457e1fa5eed498633b5a4;hpb=143498c8a5507fbf23c13a032cf9c5ebf285675d;p=e-mobility-charging-stations-simulator.git diff --git a/src/charging-station/ui-server/UIWebSocketServer.ts b/src/charging-station/ui-server/UIWebSocketServer.ts index f789d396..c067fb36 100644 --- a/src/charging-station/ui-server/UIWebSocketServer.ts +++ b/src/charging-station/ui-server/UIWebSocketServer.ts @@ -1,4 +1,4 @@ -import { IncomingMessage, createServer } from 'http'; +import type { IncomingMessage } from 'http'; import type internal from 'stream'; import { StatusCodes } from 'http-status-codes'; @@ -20,7 +20,6 @@ export default class UIWebSocketServer extends AbstractUIServer { public constructor(protected readonly uiServerConfiguration: UIServerConfiguration) { super(uiServerConfiguration); - this.httpServer = createServer(); this.webSocketServer = new WebSocketServer({ handleProtocols: UIServiceUtils.handleProtocols, noServer: true, @@ -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 */ }); @@ -88,17 +88,26 @@ export default class UIWebSocketServer extends AbstractUIServer { } } - public stop(): void { - this.chargingStations.clear(); - } - public sendRequest(request: ProtocolRequest): void { this.broadcastToClients(JSON.stringify(request)); } 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 { @@ -118,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(