1 import { IncomingMessage
, createServer
} from
'http';
2 import type internal from
'stream';
4 import { StatusCodes
} from
'http-status-codes';
5 import WebSocket
, { RawData
, WebSocketServer
} from
'ws';
7 import BaseError from
'../../exception/BaseError';
8 import type { UIServerConfiguration
} from
'../../types/ConfigurationData';
9 import type { ProtocolRequest
, ProtocolResponse
} from
'../../types/UIProtocol';
10 import { WebSocketCloseEventStatusCode
} from
'../../types/WebSocket';
11 import logger from
'../../utils/Logger';
12 import Utils from
'../../utils/Utils';
13 import { AbstractUIServer
} from
'./AbstractUIServer';
14 import UIServiceFactory from
'./ui-services/UIServiceFactory';
15 import { UIServiceUtils
} from
'./ui-services/UIServiceUtils';
17 const moduleName
= 'UIWebSocketServer';
19 export default class UIWebSocketServer
extends AbstractUIServer
{
20 private readonly webSocketServer
: WebSocketServer
;
22 public constructor(protected readonly uiServerConfiguration
: UIServerConfiguration
) {
23 super(uiServerConfiguration
);
24 this.httpServer
= createServer();
25 this.webSocketServer
= new WebSocketServer({
26 handleProtocols
: UIServiceUtils
.handleProtocols
,
31 public start(): void {
32 this.webSocketServer
.on('connection', (ws
: WebSocket
, req
: IncomingMessage
): void => {
33 const [protocol
, version
] = UIServiceUtils
.getProtocolAndVersion(ws
.protocol
);
34 if (UIServiceUtils
.isProtocolAndVersionSupported(protocol
, version
) === false) {
38 'start.server.onconnection'
39 )} Unsupported UI protocol version: '${protocol}${version}'`
41 ws
.close(WebSocketCloseEventStatusCode
.CLOSE_PROTOCOL_ERROR
);
43 if (this.uiServices
.has(version
) === false) {
44 this.uiServices
.set(version
, UIServiceFactory
.getUIServiceImplementation(version
, this));
46 ws
.on('message', (rawData
) => {
47 const [messageId
, procedureName
, payload
] = this.validateRawDataRequest(rawData
);
50 .requestHandler(this.buildProtocolRequest(messageId
, procedureName
, payload
))
52 /* Error caught by AbstractUIService */
55 ws
.on('error', (error
) => {
56 logger
.error(`${this.logPrefix(moduleName, 'start.ws.onerror')} WebSocket error:`, error
);
58 ws
.on('close', (code
, reason
) => {
63 )} WebSocket closed: '${Utils.getWebSocketCloseEventStatusString(
65 )}' - '${reason.toString()}'`
71 (req
: IncomingMessage
, socket
: internal
.Duplex
, head
: Buffer
): void => {
72 this.authenticate(req
, (err
) => {
74 socket
.write(`HTTP/1.1 ${StatusCodes.UNAUTHORIZED} Unauthorized\r\n\r\n`);
78 this.webSocketServer
.handleUpgrade(req
, socket
, head
, (ws
: WebSocket
) => {
79 this.webSocketServer
.emit('connection', ws
, req
);
84 if (this.httpServer
.listening
=== false) {
85 this.httpServer
.listen(this.uiServerConfiguration
.options
);
90 this.chargingStations
.clear();
93 public sendRequest(request
: ProtocolRequest
): void {
94 this.broadcastToClients(JSON
.stringify(request
));
97 public sendResponse(response
: ProtocolResponse
): void {
98 // TODO: send response only to the client that sent the request
99 this.broadcastToClients(JSON
.stringify(response
));
102 public logPrefix(modName
?: string, methodName
?: string, prefixSuffix
?: string): string {
103 const logMsgPrefix
= prefixSuffix
104 ? `UI WebSocket Server ${prefixSuffix}`
105 : 'UI WebSocket Server';
107 modName
&& methodName
? ` ${logMsgPrefix} | ${modName}.${methodName}:` : ` ${logMsgPrefix} |`;
108 return Utils
.logPrefix(logMsg
);
111 private broadcastToClients(message
: string): void {
112 for (const client
of this.webSocketServer
.clients
) {
113 if (client
?.readyState
=== WebSocket
.OPEN
) {
114 client
.send(message
);
119 private authenticate(req
: IncomingMessage
, next
: (err
?: Error) => void): void {
120 if (this.isBasicAuthEnabled() === true) {
121 if (this.isValidBasicAuth(req
) === false) {
122 next(new Error('Unauthorized'));
131 private validateRawDataRequest(rawData
: RawData
): ProtocolRequest
{
133 // `${this.logPrefix(
135 // 'validateRawDataRequest'
136 // )} Raw data received in string format: ${rawData.toString()}`
139 const request
= JSON
.parse(rawData
.toString()) as ProtocolRequest
;
141 if (Array.isArray(request
) === false) {
142 throw new BaseError('UI protocol request is not an array');
145 if (request
.length
!== 3) {
146 throw new BaseError('UI protocol request is malformed');