Skip to content

Commit

Permalink
feat: change reconnection logic (#383)
Browse files Browse the repository at this point in the history
  • Loading branch information
farhat-ha authored Oct 9, 2024
1 parent b2c4611 commit 8ac1d8c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 7 deletions.
13 changes: 6 additions & 7 deletions packages/js/src/Modules/Verto/BaseSession.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export default abstract class BaseSession {
throw new Error('Invalid init options');
}
this._onSocketOpen = this._onSocketOpen.bind(this);
this._onSocketCloseOrError = this._onSocketCloseOrError.bind(this);
this.onNetworkClose = this.onNetworkClose.bind(this);
this._onSocketMessage = this._onSocketMessage.bind(this);
this._handleLoginError = this._handleLoginError.bind(this);

Expand Down Expand Up @@ -248,8 +248,7 @@ export default abstract class BaseSession {
* Callback when the ws connection is going to close or get an error
* @return void
*/
protected _onSocketCloseOrError(event: any): void {
logger.error(`Socket ${event.type} ${event.message}`);
public onNetworkClose(): void {
if (this.relayProtocol) {
deRegisterAll(this.relayProtocol);
}
Expand Down Expand Up @@ -335,8 +334,8 @@ export default abstract class BaseSession {
private _attachListeners() {
this._detachListeners();
this.on(SwEvent.SocketOpen, this._onSocketOpen);
this.on(SwEvent.SocketClose, this._onSocketCloseOrError);
this.on(SwEvent.SocketError, this._onSocketCloseOrError);
this.on(SwEvent.SocketClose, this.onNetworkClose);
this.on(SwEvent.SocketError, this.onNetworkClose);
this.on(SwEvent.SocketMessage, this._onSocketMessage);
}

Expand All @@ -346,8 +345,8 @@ export default abstract class BaseSession {
*/
private _detachListeners() {
this.off(SwEvent.SocketOpen, this._onSocketOpen);
this.off(SwEvent.SocketClose, this._onSocketCloseOrError);
this.off(SwEvent.SocketError, this._onSocketCloseOrError);
this.off(SwEvent.SocketClose, this.onNetworkClose);
this.off(SwEvent.SocketError, this.onNetworkClose);
this.off(SwEvent.SocketMessage, this._onSocketMessage);
}

Expand Down
21 changes: 21 additions & 0 deletions packages/js/src/Modules/Verto/webrtc/Peer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,23 @@ export default class Peer {
this.iceGatheringComplete.resolve(true);
}
};
private handleConnectionStateChange = async (event: Event) => {
const { connectionState } = this.instance;
console.log(
`[${new Date().toISOString()}] Connection State`,
connectionState
);

if (connectionState === 'connected') {
return this.iceGatheringComplete.resolve(true);
}

if (connectionState === 'failed' || connectionState === 'disconnected') {
this.instance.restartIce();
return this._session.onNetworkClose();
}
};

private async createPeerConnection() {
this.instance = RTCPeerConnection(this._config());

Expand All @@ -190,6 +207,10 @@ export default class Peer {
this.instance.onsignalingstatechange = this.handleSignalingStateChangeEvent;
this.instance.onnegotiationneeded = this.handleNegotiationNeededEvent;
this.instance.ontrack = this.handleTrackEvent;
this.instance.addEventListener(
'connectionstatechange',
this.handleConnectionStateChange
);
this.instance.addEventListener('icecandidate', this.handleIceCandidate);
this.instance.addEventListener(
'iceconnectionstatechange',
Expand Down

0 comments on commit 8ac1d8c

Please sign in to comment.