ad3847790149823eb9859474b165ab0e878aa389
1 import { IncomingMessage
} from
'http';
3 import WebSocket from
'ws';
5 import { ServerOptions
} from
'../../types/ConfigurationData';
6 import { Protocol
, ProtocolVersion
} from
'../../types/UIProtocol';
7 import Configuration from
'../../utils/Configuration';
8 import logger from
'../../utils/Logger';
9 import Utils from
'../../utils/Utils';
10 import { AbstractUIServer
} from
'./AbstractUIServer';
11 import UIServiceFactory from
'./ui-services/UIServiceFactory';
13 export default class UIWebSocketServer
extends AbstractUIServer
{
14 public constructor(options
?: ServerOptions
) {
16 this.server
= new WebSocket
.Server(options
?? Configuration
.getUIServer().options
);
19 public start(): void {
20 this.server
.on('connection', (socket
: WebSocket
, request
: IncomingMessage
): void => {
21 const protocolIndex
= socket
.protocol
.indexOf(Protocol
.UI
);
22 const version
= socket
.protocol
.substring(
23 protocolIndex
+ Protocol
.UI
.length
25 if (!this.uiServices
.has(version
)) {
26 this.uiServices
.set(version
, UIServiceFactory
.getUIServiceImplementation(version
, this));
28 // FIXME: check connection validity
29 socket
.on('message', (messageData
) => {
32 .messageHandler(messageData
)
34 logger
.error(`${this.logPrefix()} Error while handling message data: %j`, messageData
);
37 socket
.on('error', (error
) => {
38 logger
.error(`${this.logPrefix()} Error on WebSocket: %j`, error
);
47 public sendResponse(message
: string): void {
48 this.broadcastToClients(message
);
51 public logPrefix(): string {
52 return Utils
.logPrefix(' UI WebSocket Server:');
55 private broadcastToClients(message
: string): void {
56 for (const client
of (this.server
as WebSocket
.Server
).clients
) {
57 if (client
?.readyState
=== WebSocket
.OPEN
) {