Skip to content

Commit

Permalink
fix: improved error handling when connecting to an SFU (#1648)
Browse files Browse the repository at this point in the history
Make sure we bubble the potential errors coming out of the initial SFU
signal server connection establishment.

---------

Co-authored-by: Matvei Andrienko <[email protected]>
  • Loading branch information
oliverlaz and myandrienko authored Jan 20, 2025
1 parent 2e258a1 commit 27332b4
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
13 changes: 10 additions & 3 deletions packages/client/src/StreamSfuClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,16 +227,23 @@ export class StreamSfuClient {
},
});

this.signalWs.addEventListener('close', this.handleWebSocketClose);

this.signalReady = makeSafePromise(
Promise.race<WebSocket>([
new Promise((resolve) => {
new Promise((resolve, reject) => {
const onOpen = () => {
this.signalWs.removeEventListener('open', onOpen);
resolve(this.signalWs);
};

this.signalWs.addEventListener('open', onOpen);

this.signalWs.addEventListener('close', () => {
this.handleWebSocketClose();
// Normally, this shouldn't have any effect, because WS should never emit 'close'
// before emitting 'open'. However, strager things have happened, and we don't
// want to leave signalReady in pending state.
reject(new Error('SFU WS closed unexpectedly'));
});
}),

new Promise((resolve, reject) => {
Expand Down
1 change: 1 addition & 0 deletions packages/client/src/rtc/signal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ export const createWebSocketSignalChannel = (opts: {
}) => {
const { endpoint, onMessage, logTag } = opts;
const logger = getLogger(['SfuClientWS', logTag]);
logger('debug', 'Creating signaling WS channel:', endpoint);
const ws = new WebSocket(endpoint);
ws.binaryType = 'arraybuffer'; // do we need this?

Expand Down

0 comments on commit 27332b4

Please sign in to comment.