- // TODO: send response only to the client that sent the request
- this.broadcastToClients(JSON.stringify(response));
+ const responseId = response[0];
+ try {
+ if (this.hasResponseHandler(responseId)) {
+ const ws = this.responseHandlers.get(responseId) as WebSocket;
+ if (ws?.readyState === WebSocket.OPEN) {
+ ws.send(JSON.stringify(response));
+ } else {
+ logger.error(
+ `${this.logPrefix(
+ moduleName,
+ 'sendResponse',
+ )} Error at sending response id '${responseId}', WebSocket is not open: ${ws?.readyState}`,
+ );
+ }
+ } else {
+ logger.error(
+ `${this.logPrefix(
+ moduleName,
+ 'sendResponse',
+ )} Response for unknown request id: ${responseId}`,
+ );
+ }
+ } catch (error) {
+ logger.error(
+ `${this.logPrefix(
+ moduleName,
+ 'sendResponse',
+ )} Error at sending response id '${responseId}':`,
+ error,
+ );
+ } finally {
+ this.responseHandlers.delete(responseId);
+ }