Skip to content

Commit

Permalink
Consolidate ws close action (#685)
Browse files Browse the repository at this point in the history
* Consolidate ws close action

* Create hot-insects-teach.md
  • Loading branch information
lukasIO committed May 3, 2023
1 parent 07bf567 commit 8088387
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/hot-insects-teach.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"livekit-client": patch
---

Consolidate ws close action
21 changes: 10 additions & 11 deletions src/api/SignalClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@ export class SignalClient {
};

this.ws.onclose = (ev: CloseEvent) => {
log.warn(`websocket closed`, { ev });
this.handleOnClose(ev.reason);
};
});
Expand All @@ -344,12 +345,14 @@ export class SignalClient {
}
});

this.ws.close();
// 250ms grace period for ws to close gracefully
await Promise.race([closePromise, sleep(250)]);
if (this.ws.readyState < this.ws.CLOSING) {
this.ws.close();
// 250ms grace period for ws to close gracefully
await Promise.race([closePromise, sleep(250)]);
}
this.ws = undefined;
this.clearPingInterval();
}
this.ws = undefined;
this.clearPingInterval();
} finally {
unlock();
}
Expand Down Expand Up @@ -611,17 +614,13 @@ export class SignalClient {
this.isReconnecting = false;
}

private handleOnClose(reason: string) {
private async handleOnClose(reason: string) {
if (!this.isConnected) return;
this.clearPingInterval();
this.clearPingTimeout();

await this.close();
log.debug(`websocket connection closed: ${reason}`);
this.isConnected = false;
if (this.onClose) {
this.onClose(reason);
}
this.ws = undefined;
}

private handleWSError(ev: Event) {
Expand Down

0 comments on commit 8088387

Please sign in to comment.