);
ws.close(WebSocketCloseEventStatusCode.CLOSE_PROTOCOL_ERROR);
}
- this.sockets.add(ws);
this.registerProtocolVersionUIService(version);
ws.on('message', (rawData) => {
const request = this.validateRawDataRequest(rawData);
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 */
});
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 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(