-import { IncomingMessage, createServer } from 'http';
+import type { IncomingMessage } from 'http';
import type internal from 'stream';
import { StatusCodes } from 'http-status-codes';
public constructor(protected readonly uiServerConfiguration: UIServerConfiguration) {
super(uiServerConfiguration);
- this.httpServer = createServer();
this.webSocketServer = new WebSocketServer({
handleProtocols: UIServiceUtils.handleProtocols,
noServer: true,
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 */
});
}
}
- 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 {
}
}
- 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(