1 import { Protocol
, ProtocolCommand
, ProtocolRequest
, ProtocolVersion
} from
'../types/UIProtocol';
2 import WebSocket
, { OPEN
, Server
, ServerOptions
} from
'ws';
4 import AbstractUIService from
'./UIWebSocketServices/AbstractUIService';
5 import BaseError from
'../exception/BaseError';
6 import Configuration from
'../utils/Configuration';
7 import { IncomingMessage
} from
'http';
8 import UIServiceFactory from
'./UIWebSocketServices/UIServiceFactory';
9 import Utils from
'../utils/Utils';
10 import logger from
'../utils/Logger';
12 export default class UIWebSocketServer
extends Server
{
13 public uiService
: AbstractUIService
;
15 public constructor(options
?: ServerOptions
, callback
?: () => void) {
16 // Create the WebSocket Server
17 super(options
?? Configuration
.getUIWebSocketServer().options
, callback
);
20 public broadcastToClients(message
: string | Record
<string, unknown
>): void {
21 for (const client
of this.clients
) {
22 if (client
?.readyState
=== OPEN
) {
28 public start(): void {
29 this.on('connection', (socket
: WebSocket
, request
: IncomingMessage
): void => {
30 const protocolIndex
= socket
.protocol
.indexOf(Protocol
.UI
);
31 const version
= socket
.protocol
.substring(protocolIndex
+ Protocol
.UI
.length
) as ProtocolVersion
;
32 this.uiService
= UIServiceFactory
.getUIServiceImplementation(version
, this);
33 if (!this.uiService
) {
34 throw new BaseError(`Could not find a UI service implementation for UI protocol version ${version}`);
36 // FIXME: check connection validity
37 socket
.on('message', (messageData
) => {
38 let [command
, payload
]: ProtocolRequest
= [ProtocolCommand
.UNKNOWN
, {}];
39 const protocolRequest
= JSON
.parse(messageData
.toString()) as ProtocolRequest
;
40 if (Utils
.isIterable(protocolRequest
)) {
41 [command
, payload
] = protocolRequest
;
43 throw new BaseError('UI protocol request is not iterable');
45 this.uiService
.handleMessage(command
, payload
).catch(() => {
46 logger
.error(`${this.logPrefix()} Error while handling command %s message: %j`, command
, payload
);
49 socket
.on('error', (error
) => {
50 logger
.error(`${this.logPrefix()} Error on WebSocket: %j`, error
);
59 public logPrefix(): string {
60 return Utils
.logPrefix(' UI WebSocket Server:');