-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* 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
Showing
2 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`); | ||
} | ||
} |