Skip to content

Commit

Permalink
feat: add connect in spaceV2 (#711)
Browse files Browse the repository at this point in the history
* feat: add connect in spaceV2

* refactor: fn fix

* fix: added async

* refactor: added file level comment

* refactor: changed incoming to pending array

* fix: var name

* refactor: change request to invite
  • Loading branch information
arn4b authored Sep 15, 2023
1 parent 54b215d commit 76dab91
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
@@ -1,6 +1,7 @@
import { produce } from "immer";

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

Expand Down Expand Up @@ -161,6 +162,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 76dab91

Please sign in to comment.