Skip to content

Commit

Permalink
send out connecting related events immediately'
Browse files Browse the repository at this point in the history
  • Loading branch information
czerwiukk committed Jan 7, 2025
1 parent 03ecccf commit b57dcf0
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import {
useScreenShare,
} from "@fishjam-cloud/react-client";
import {
LucideAirVent,
Mic,
MicOff,
MonitorOff,
Expand Down Expand Up @@ -50,8 +49,6 @@ export const CallToolbar = () => {
}
};

const fuckupWebsocket = () => {};

return (
<footer className="flex h-24 items-center justify-center gap-8 border-t border-stone-200">
<SettingsSheet>
Expand Down Expand Up @@ -82,10 +79,6 @@ export const CallToolbar = () => {
<ScreenshareIcon size={20} strokeWidth={"1.5px"} />
</Button>

<Button className="gap-2 text-xs" onClick={fuckupWebsocket}>
<LucideAirVent size={20} strokeWidth={"1.5px"} />
</Button>

<Button
className="ml-4 gap-2 text-xs"
variant="destructive"
Expand Down
17 changes: 10 additions & 7 deletions packages/ts-client/src/FishjamClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,6 @@ export class FishjamClient<PeerMetadata = GenericMetadata, ServerMetadata = Gene

this.initWebsocket(peerMetadata);
this.setupCallbacks();
console.log('CONNECTED!');
this.status = 'initialized';
}

Expand All @@ -160,8 +159,6 @@ export class FishjamClient<PeerMetadata = GenericMetadata, ServerMetadata = Gene

const sdkVersion = `web-${packageVersion}`;
const message = PeerMessage.encode({ authRequest: { token, sdkVersion } }).finish();
console.log('socket opened', this.websocket);
console.log('sending auth rewquest', { authRequest: { token, sdkVersion } });
this.websocket?.send(message);
};

Expand All @@ -187,14 +184,12 @@ export class FishjamClient<PeerMetadata = GenericMetadata, ServerMetadata = Gene
try {
const data = PeerMessage.decode(uint8Array);
const serverMediaEvent = data.serverMediaEvent;

if (data.authenticated) {
this.emit('authSuccess');
this.webrtc?.connect(peerMetadata);
} else if (data.authRequest) {
console.warn('Received unexpected control message: authRequest');
} else if (serverMediaEvent) {
console.log('server event', serverMediaEvent);
this.webrtc?.receiveMediaEvent(ServerMediaEvent.encode(serverMediaEvent).finish());
}
} catch (e) {
Expand Down Expand Up @@ -264,15 +259,23 @@ export class FishjamClient<PeerMetadata = GenericMetadata, ServerMetadata = Gene
return this.webrtc?.getBandwidthEstimation();
}

private isConnectingRelatedEvent(mediaEvent: PeerMediaEvent) {
return Boolean(mediaEvent.connect || mediaEvent.renegotiateTracks || mediaEvent.sdpOffer || mediaEvent.candidate);
}

private setupCallbacks() {
this.webrtc?.on('sendMediaEvent', (mediaEvent) => {
const peerMediaEvent = PeerMediaEvent.decode(mediaEvent);
const serializedEvent = PeerMessage.encode({ peerMediaEvent }).finish();
this.eventQueue.enqueueEvent(serializedEvent);

if (this.isConnectingRelatedEvent(peerMediaEvent)) {
this.websocket?.send(serializedEvent);
} else {
this.eventQueue.enqueueEvent(serializedEvent);
}
});

this.webrtc?.on('connected', async (peerId: string, endpointsInRoom: Endpoint[]) => {
console.log('webrtc connected');
const peers = endpointsInRoom
.filter((endpoint) => isPeer(endpoint))
.map((peer) => peer as Peer<PeerMetadata, ServerMetadata>);
Expand Down
2 changes: 0 additions & 2 deletions packages/ts-client/src/eventQueue.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ export class EventQueue {

public enqueueEvent(mediaEvent: Uint8Array) {
this.queuedEvents.push(mediaEvent);
console.log('enqueuingEvent');
if (this.currentTimeout) return;
this.attemptToSendEvent();
}
Expand All @@ -33,7 +32,6 @@ export class EventQueue {
if (isReconnecting) {
this.setupTimeout(currentAttempt + 1);
} else {
console.log('reconnected, events in queue: ', this.enqueueEvent.length);
this.attemptToSendEvent();
}
}, delay);
Expand Down
10 changes: 1 addition & 9 deletions packages/ts-client/src/reconnection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const DEFAULT_RECONNECT_CONFIG: Required<ReconnectConfig> = {
maxAttempts: 3,
initialDelay: 500,
delay: 500,
addTracksOnReconnect: true,
addTracksOnReconnect: false,
};

export class ReconnectManager<PeerMetadata, ServerMetadata> {
Expand Down Expand Up @@ -75,9 +75,7 @@ export class ReconnectManager<PeerMetadata, ServerMetadata> {
this.client.on('connectionError', onConnectionError);

const onSocketClose: MessageEvents<PeerMetadata, ServerMetadata>['socketClose'] = (event) => {
console.log(event);
if (isAuthError(event.reason)) return;
console.log('reconnecting');
this.reconnect();
};
this.client.on('socketClose', onSocketClose);
Expand All @@ -96,7 +94,6 @@ export class ReconnectManager<PeerMetadata, ServerMetadata> {
}

public isReconnecting(): boolean {
console.log('status is ', this.status);
return this.status === 'reconnecting';
}

Expand All @@ -115,7 +112,6 @@ export class ReconnectManager<PeerMetadata, ServerMetadata> {
if (this.reconnectTimeoutId) return;

if (this.reconnectAttempt >= this.reconnectConfig.maxAttempts) {
console.log('reconnection limit reached');
if (this.status === 'reconnecting') {
this.status = 'error';

Expand All @@ -126,7 +122,6 @@ export class ReconnectManager<PeerMetadata, ServerMetadata> {

if (this.status !== 'reconnecting') {
this.status = 'reconnecting';
console.log('setting status to reconnecting');
this.client.emit('reconnectionStarted');

this.lastLocalEndpoint = this.client.getLocalPeer() || null;
Expand All @@ -136,18 +131,15 @@ export class ReconnectManager<PeerMetadata, ServerMetadata> {

this.reconnectAttempt += 1;

console.log('setting timeout');
this.reconnectTimeoutId = setTimeout(() => {
this.reconnectTimeoutId = null;

console.log('connecting again');
this.connect(this.getLastPeerMetadata() ?? this.initialMetadata!);
}, timeout);
}

public async handleReconnect() {
if (this.status !== 'reconnecting') return;
console.log('readding tracks');
if (this.lastLocalEndpoint && this.reconnectConfig.addTracksOnReconnect) {
for await (const element of this.lastLocalEndpoint.tracks) {
const [_, track] = element;
Expand Down

0 comments on commit b57dcf0

Please sign in to comment.