From 76dab912f3684857a38265e3c2351a40476393e0 Mon Sep 17 00:00:00 2001 From: Arnab Chatterjee <60937304+arn4b@users.noreply.github.com> Date: Fri, 15 Sep 2023 15:50:19 +0530 Subject: [PATCH] feat: add connect in spaceV2 (#711) * 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 --- packages/restapi/src/lib/spaceV2/SpaceV2.ts | 2 + packages/restapi/src/lib/spaceV2/connect.ts | 71 +++++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100644 packages/restapi/src/lib/spaceV2/connect.ts diff --git a/packages/restapi/src/lib/spaceV2/SpaceV2.ts b/packages/restapi/src/lib/spaceV2/SpaceV2.ts index c892ef33c..04f37c0c5 100644 --- a/packages/restapi/src/lib/spaceV2/SpaceV2.ts +++ b/packages/restapi/src/lib/spaceV2/SpaceV2.ts @@ -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"; @@ -161,6 +162,7 @@ export class SpaceV2 { } async connect(options: any) { + await connect.call(this, options); /** * will contain logic to handle all connections */ diff --git a/packages/restapi/src/lib/spaceV2/connect.ts b/packages/restapi/src/lib/spaceV2/connect.ts new file mode 100644 index 000000000..f399096fb --- /dev/null +++ b/packages/restapi/src/lib/spaceV2/connect.ts @@ -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}`); + } +}