Skip to content

Commit

Permalink
fix: add timeout for promise waiting for ws open
Browse files Browse the repository at this point in the history
  • Loading branch information
santhoshvai committed Sep 20, 2024
1 parent 0d27e7b commit 9dcf1e7
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion packages/client/src/StreamSfuClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,16 @@ export class StreamSfuClient {
* @param timeout an optional timeout in milliseconds for sending the message.
*/
private send = async (message: SfuRequest, timeout: number = 0) => {
await this.signalReady; // wait for the signal ws to be open
if (timeout > 0) {
await Promise.race([
this.signalReady,
new Promise((_, reject) => {
setTimeout(() => reject(new Error('Timeout sending msg')), timeout);
}),
]);
} else {
await this.signalReady; // wait for the signal ws to be open
}
const msgJson = SfuRequest.toJson(message);
if (this.signalWs.readyState !== WebSocket.OPEN) {
this.logger('debug', 'Signal WS is not open. Skipping message', msgJson);
Expand Down

0 comments on commit 9dcf1e7

Please sign in to comment.