repositories
/
e-mobility-charging-stations-simulator.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
UI service attribute rename: workerBroadcastChannel -> uiServiceWorkerBroadcastChannel
[e-mobility-charging-stations-simulator.git]
/
src
/
charging-station
/
ui-server
/
UIWebSocketServer.ts
diff --git
a/src/charging-station/ui-server/UIWebSocketServer.ts
b/src/charging-station/ui-server/UIWebSocketServer.ts
index fcfc309708811f88eeef7857c6dfec13639f0ce4..5f02f2ea47f9de93276970d53672e078a7fbc9b8 100644
(file)
--- a/
src/charging-station/ui-server/UIWebSocketServer.ts
+++ b/
src/charging-station/ui-server/UIWebSocketServer.ts
@@
-1,22
+1,25
@@
-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 { IncomingMessage } from 'http';
+
+import WebSocket from 'ws';
+
import { ServerOptions } from '../../types/ConfigurationData';
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 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();
export default class UIWebSocketServer extends AbstractUIServer {
public constructor(options?: ServerOptions) {
super();
- this.
uiServer = new
Server(options ?? Configuration.getUIServer().options);
+ this.
server = new WebSocket.
Server(options ?? Configuration.getUIServer().options);
}
public start(): void {
}
public start(): void {
- this.
uiS
erver.on('connection', (socket: WebSocket, request: IncomingMessage): void => {
+ this.
s
erver.on('connection', (socket: WebSocket, request: IncomingMessage): void => {
const protocolIndex = socket.protocol.indexOf(Protocol.UI);
const version = socket.protocol.substring(
protocolIndex + Protocol.UI.length
const protocolIndex = socket.protocol.indexOf(Protocol.UI);
const version = socket.protocol.substring(
protocolIndex + Protocol.UI.length
@@
-25,35
+28,46
@@
export default class UIWebSocketServer extends AbstractUIServer {
this.uiServices.set(version, UIServiceFactory.getUIServiceImplementation(version, this));
}
// FIXME: check connection validity
this.uiServices.set(version, UIServiceFactory.getUIServiceImplementation(version, this));
}
// FIXME: check connection validity
- socket.on('message', (
message
Data) => {
+ socket.on('message', (
raw
Data) => {
this.uiServices
.get(version)
this.uiServices
.get(version)
- .
messageHandler(message
Data)
+ .
requestHandler(raw
Data)
.catch(() => {
.catch(() => {
- logger.error(`${this.logPrefix()} Error while handling message data: %j`, messageData);
+ /* Error caught by AbstractUIService */
});
});
socket.on('error', (error) => {
});
});
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
+ );
});
});
}
public stop(): void {
});
});
}
public stop(): void {
- this.uiServer.close();
+ this.server.close();
+ }
+
+ public sendRequest(request: string): void {
+ this.broadcastToClients(request);
}
}
- public sendResponse(
messag
e: string): void {
- this.broadcastToClients(
messag
e);
+ public sendResponse(
respons
e: string): void {
+ this.broadcastToClients(
respons
e);
}
}
- 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 {
}
private broadcastToClients(message: string): void {
- for (const client of (this.
uiServer 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);
}
}
client.send(message);
}
}