Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: change reconnection logic #383

Merged
merged 1 commit into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
);

Comment on lines +187 to +191
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if this console.log is supposed to stay so i'm just leaving this comment as a doble check.

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
Loading