Skip to content

Commit

Permalink
FCE-1059/fix reconnection manager endpoint metadata retrieval (#251)
Browse files Browse the repository at this point in the history
## Description

Fixes low level peer metadata retrieval from RTC Endpoint.

## Motivation and Context

The tsclient's `ReconnectionManager` was still unaware of the metadata
being divided between peer and server.
The type of the metadata at the `LocalTrack` level is `unknown` and
`ReconnectionManager` was wrongfully casting it as `PeerMetadata`.
This led to issues with providing correct metadata while reconnecting.

## Types of changes

- [x] Bug fix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing
functionality to
      not work as expected)
  • Loading branch information
czerwiukk authored Jan 21, 2025
1 parent c8f08b4 commit 8636878
Showing 1 changed file with 9 additions and 7 deletions.
16 changes: 9 additions & 7 deletions packages/ts-client/src/reconnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { Endpoint } from '@fishjam-cloud/webrtc-client';

import { isAuthError } from './auth';
import type { FishjamClient } from './FishjamClient';
import type { MessageEvents, TrackMetadata } from './types';
import type { MessageEvents, Metadata, TrackMetadata } from './types';

export type ReconnectionStatus = 'reconnecting' | 'idle' | 'error';

Expand Down Expand Up @@ -46,7 +46,7 @@ export class ReconnectManager<PeerMetadata, ServerMetadata> {

private readonly connect: (metadata: PeerMetadata) => void;
private readonly client: FishjamClient<PeerMetadata, ServerMetadata>;
private initialMetadata: PeerMetadata | undefined | null = undefined;
private initialPeerMetadata: PeerMetadata | undefined | null = undefined;

private reconnectAttempt: number = 0;
private reconnectTimeoutId: NodeJS.Timeout | null = null;
Expand Down Expand Up @@ -80,7 +80,7 @@ export class ReconnectManager<PeerMetadata, ServerMetadata> {
this.client.on('socketClose', onSocketClose);

const onAuthSuccess: MessageEvents<PeerMetadata, ServerMetadata>['authSuccess'] = () => {
this.reset(this.initialMetadata!);
this.reset(this.initialPeerMetadata!);
};
this.client.on('authSuccess', onAuthSuccess);

Expand All @@ -96,15 +96,16 @@ export class ReconnectManager<PeerMetadata, ServerMetadata> {
return this.status === 'reconnecting';
}

public reset(initialMetadata: PeerMetadata) {
this.initialMetadata = initialMetadata;
public reset(initialPeerMetadata: PeerMetadata) {
this.initialPeerMetadata = initialPeerMetadata;
this.reconnectAttempt = 0;
if (this.reconnectTimeoutId) clearTimeout(this.reconnectTimeoutId);
this.reconnectTimeoutId = null;
}

private getLastPeerMetadata(): PeerMetadata | undefined {
return this.lastLocalEndpoint?.metadata as PeerMetadata;
const endpointMetadata = this.lastLocalEndpoint?.metadata as Metadata<PeerMetadata, ServerMetadata> | undefined;
return endpointMetadata?.peer;
}

private reconnect() {
Expand Down Expand Up @@ -133,7 +134,8 @@ export class ReconnectManager<PeerMetadata, ServerMetadata> {
this.reconnectTimeoutId = setTimeout(() => {
this.reconnectTimeoutId = null;

this.connect(this.getLastPeerMetadata() ?? this.initialMetadata!);
const peerMetadata = this.getLastPeerMetadata() ?? this.initialPeerMetadata!;
this.connect(peerMetadata);
}, timeout);
}

Expand Down

0 comments on commit 8636878

Please sign in to comment.