- let uuid: string;
- return Utils.promiseWithTimeout(
- new Promise((resolve, reject) => {
- uuid = crypto.randomUUID();
- const msg = JSON.stringify([uuid, command, data]);
-
- if (this.ws.readyState !== WebSocket.OPEN) {
- this.openWS();
- }
- if (this.ws.readyState === WebSocket.OPEN) {
- this.ws.send(msg);
- } else {
- throw new Error(`Send request '${command}' message: connection not opened`);
+ return new Promise<ResponsePayload>((resolve, reject) => {
+ if (this.ws.readyState !== WebSocket.OPEN) {
+ this.openWS()
+ }
+ if (this.ws.readyState === WebSocket.OPEN) {
+ const uuid = crypto.randomUUID()
+ const msg = JSON.stringify([uuid, command, data])
+ const sendTimeout = setTimeout(() => {
+ this.deleteResponseHandler(uuid)
+ return reject(new Error(`Send request '${command}' message timeout`))
+ }, 60 * 1000)
+ try {
+ this.ws.send(msg)
+ this.setResponseHandler(uuid, command, resolve, reject)
+ } catch (error) {
+ this.deleteResponseHandler(uuid)
+ reject(error)
+ } finally {
+ clearTimeout(sendTimeout)