-import type { IncomingMessage, Server } from 'http';
+import { type IncomingMessage, Server } from 'http';
+import type { Socket } from 'net';
+
+import type { WebSocket } from 'ws';
import type { ChargingStationData } from '../../types/ChargingStationWorker';
import type { UIServerConfiguration } from '../../types/ConfigurationData';
export abstract class AbstractUIServer {
public readonly chargingStations: Map<string, ChargingStationData>;
protected httpServer: Server;
+ protected sockets: Set<Socket | WebSocket>;
protected readonly uiServices: Map<ProtocolVersion, AbstractUIService>;
public constructor(protected readonly uiServerConfiguration: UIServerConfiguration) {
this.chargingStations = new Map<string, ChargingStationData>();
+ this.httpServer = new Server();
+ this.sockets = new Set<Socket>();
this.uiServices = new Map<ProtocolVersion, AbstractUIService>();
}
return [id, responsePayload];
}
+ public stop(): void {
+ this.chargingStations.clear();
+ }
+
protected registerProtocolVersionUIService(version: ProtocolVersion): void {
if (this.uiServices.has(version) === false) {
this.uiServices.set(version, UIServiceFactory.getUIServiceImplementation(version, this));
}
public abstract start(): void;
- public abstract stop(): void;
public abstract sendRequest(request: ProtocolRequest): void;
public abstract sendResponse(response: ProtocolResponse): void;
public abstract logPrefix(
-import { IncomingMessage, RequestListener, Server, ServerResponse } from 'http';
+import type { IncomingMessage, RequestListener, ServerResponse } from 'http';
import { StatusCodes } from 'http-status-codes';
public constructor(protected readonly uiServerConfiguration: UIServerConfiguration) {
super(uiServerConfiguration);
- this.httpServer = new Server(this.requestListener.bind(this) as RequestListener);
this.responseHandlers = new Map<string, responseHandler>();
}
public start(): void {
+ this.httpServer.on('connection', (socket) => {
+ this.sockets.add(socket);
+ socket.on('close', () => {
+ this.sockets.delete(socket);
+ });
+ });
+ this.httpServer.on('request', this.requestListener.bind(this) as RequestListener);
if (this.httpServer.listening === false) {
this.httpServer.listen(this.uiServerConfiguration.options);
}
}
- public stop(): void {
- this.chargingStations.clear();
- }
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
public sendRequest(request: ProtocolRequest): void {
// This is intentionally left blank
res.writeHead(this.responseStatusToStatusCode(payload.status), {
'Content-Type': 'application/json',
});
- res.write(JSON.stringify(payload));
- res.end();
+ res.end(JSON.stringify(payload));
this.responseHandlers.delete(uuid);
} else {
logger.error(
if (UIServiceUtils.isProtocolAndVersionSupported(protocol, version) === false) {
throw new BaseError(`Unsupported UI protocol version: '/${protocol}/${version}'`);
}
+ this.registerProtocolVersionUIService(version);
req.on('error', (error) => {
logger.error(
`${this.logPrefix(moduleName, 'requestListener.req.onerror')} Error on HTTP request:`,
error
);
});
- this.registerProtocolVersionUIService(version);
if (req.method === 'POST') {
const bodyBuffer = [];
req
-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_PROTOCOL_ERROR);
}
+ this.sockets.add(ws);
this.registerProtocolVersionUIService(version);
ws.on('message', (rawData) => {
const request = this.validateRawDataRequest(rawData);
logger.error(`${this.logPrefix(moduleName, 'start.ws.onerror')} WebSocket error:`, error);
});
ws.on('close', (code, reason) => {
+ this.sockets.delete(ws);
logger.debug(
`${this.logPrefix(
moduleName,
}
}
- public stop(): void {
- this.chargingStations.clear();
- }
-
public sendRequest(request: ProtocolRequest): void {
this.broadcastToClients(JSON.stringify(request));
}