Skip to content

Commit

Permalink
Merge branch 'feat/space-v2' into refactor/add-start
Browse files Browse the repository at this point in the history
  • Loading branch information
0xNilesh authored Sep 15, 2023
2 parents baa44e3 + 76dab91 commit d463b6d
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/restapi/src/lib/spaceV2/SpaceV2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { produce } from "immer";

import { join } from "./join";
import { start } from "./start";
import { connect } from "./connect";
import { acceptInvite } from "./acceptInvite";
import { ISpaceInviteInputOptions, inviteToJoin } from "./inviteToJoin";

Expand Down Expand Up @@ -162,6 +163,7 @@ export class SpaceV2 {
}

async connect(options: any) {
await connect.call(this, options);
/**
* will contain logic to handle all connections
*/
Expand Down
71 changes: 71 additions & 0 deletions packages/restapi/src/lib/spaceV2/connect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/**
* connect.ts
*
* The 'connect' function is responsible for establishing peer connections
*
* @param {IConnectOptions} options - An object containing signal data and a peer address.
*/
import { produce } from "immer";

import { SpaceV2 } from "./SpaceV2";

import { VideoCallStatus } from "../types";
import getIncomingIndexFromAddress from "../video/helpers/getIncomingIndexFromAddress";

export interface IConnectOptions {
signalData: any;
peerAddress: string;
}

export async function connect(
this: SpaceV2,
options: IConnectOptions
) {
const { peerAddress, signalData } = options || {};

try {
const peerConnection = this.getPeerConnection(
peerAddress ? peerAddress : this.data.pendingPeerStreams[0].address
) as any;

peerConnection?.on('error', (err: any) => {
console.log('error in connect', err);

const pendingIndex = peerAddress
? getIncomingIndexFromAddress(this.data.pendingPeerStreams, peerAddress)
: 0;

if (this.data.pendingPeerStreams[pendingIndex].retryCount >= 5) {
console.log('Max retries exceeded, please try again.');
this.disconnect({
peerAddress: peerAddress
? peerAddress
: this.data.pendingPeerStreams[0].address,
});
}

// retrying in case of connection error
this.invite({
senderAddress: this.data.local.address,
recipientAddress: this.data.pendingPeerStreams[pendingIndex].address,
spaceId: this.data.spaceInfo.spaceId,
retry: true,
});
})

peerConnection?.signal(signalData);

// update space data
this.setSpaceV2Data((oldSpaceData) => {
return produce(oldSpaceData, (draft) => {
const pendingIndex = peerAddress
? getIncomingIndexFromAddress(oldSpaceData.pendingPeerStreams, peerAddress)
: 0;
draft.pendingPeerStreams[pendingIndex].status = VideoCallStatus.CONNECTED;
});
});
} catch (err) {
console.error(`[Push SDK] - API - Error - API ${connect.name} -: `, err);
throw Error(`[Push SDK] - API - Error - API ${connect.name} -: ${err}`);
}
}

0 comments on commit d463b6d

Please sign in to comment.