X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2Fcharging-station%2Fui-server%2FUIWebSocketServer.ts;h=5f02f2ea47f9de93276970d53672e078a7fbc9b8;hb=6812b4e11d45a0d6179a266687c7ad9aa6e3b538;hp=f71f404b28b3bce0c11126eb607ba56381b693f6;hpb=d200b695603b334df62cd82a10f1a73f88fbf54b;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 f71f404b..5f02f2ea 100644 --- a/src/charging-station/ui-server/UIWebSocketServer.ts +++ b/src/charging-station/ui-server/UIWebSocketServer.ts @@ -1,18 +1,21 @@ -import { Protocol, ProtocolVersion } from '../../types/UIProtocol'; -import WebSocket, { OPEN, Server } from 'ws'; - -import { AbstractUIServer } from './AbstractUIServer'; -import Configuration from '../../utils/Configuration'; import { IncomingMessage } from 'http'; + +import WebSocket from 'ws'; + import { ServerOptions } from '../../types/ConfigurationData'; -import UIServiceFactory from './ui-services/UIServiceFactory'; -import Utils from '../../utils/Utils'; +import { Protocol, ProtocolVersion } from '../../types/UIProtocol'; +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'; + +const moduleName = 'UIWebSocketServer'; export default class UIWebSocketServer extends AbstractUIServer { public constructor(options?: ServerOptions) { super(); - this.server = new Server(options ?? Configuration.getUIServer().options); + this.server = new WebSocket.Server(options ?? Configuration.getUIServer().options); } public start(): void { @@ -25,16 +28,19 @@ export default class UIWebSocketServer extends AbstractUIServer { this.uiServices.set(version, UIServiceFactory.getUIServiceImplementation(version, this)); } // FIXME: check connection validity - socket.on('message', (messageData) => { + socket.on('message', (rawData) => { this.uiServices .get(version) - .messageHandler(messageData) + .requestHandler(rawData) .catch(() => { - logger.error(`${this.logPrefix()} Error while handling message data: %j`, messageData); + /* Error caught by AbstractUIService */ }); }); socket.on('error', (error) => { - logger.error(`${this.logPrefix()} Error on WebSocket: %j`, error); + logger.error( + `${this.logPrefix(moduleName, 'start.socket.onerror')} Error on WebSocket:`, + error + ); }); }); } @@ -43,17 +49,25 @@ export default class UIWebSocketServer extends AbstractUIServer { this.server.close(); } - public sendResponse(message: string): void { - this.broadcastToClients(message); + public sendRequest(request: string): void { + this.broadcastToClients(request); + } + + public sendResponse(response: string): void { + this.broadcastToClients(response); } - public logPrefix(): string { - return Utils.logPrefix(' UI WebSocket Server:'); + public logPrefix(modName?: string, methodName?: string): string { + const logMsg = + modName && methodName + ? ` UI WebSocket Server | ${modName}.${methodName}:` + : ' UI WebSocket Server |'; + return Utils.logPrefix(logMsg); } private broadcastToClients(message: string): void { - for (const client of (this.server as Server).clients) { - if (client?.readyState === OPEN) { + for (const client of (this.server as WebSocket.Server).clients) { + if (client?.readyState === WebSocket.OPEN) { client.send(message); } }