Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add connect in spaceV2 #711

Merged
merged 7 commits into from
Sep 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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}`);
}
}