呼叫
功能介绍
实现用户之间的呼叫功能,流程是:A 用户先创建一个房间,然后呼叫 B 用户,如果 B 用户接受呼叫,AB 进入房间进行通讯。
注意:
在登录成功并且启用呼叫功能的情况下才可以使用呼叫功能
注册回调
import { CrSDKNotifier, NOTIFIER_EVENT } from '@cr/react-native-rtc';
class CallListener extends CrSDKNotifier {
// 收到呼叫
notifyCallIn(notify: { callID: string; callerID: string; usrExtDat?: string }) {
// notify.callID:呼叫ID
// notify.callerID:呼叫者ID
// 可在此处选择接受或拒绝呼叫
}
// 对方接受了呼叫
notifyCallAccepted(notify: { callID: string; usrExtDat?: string }) {
// 可进入房间
}
// 对方拒绝了呼叫
notifyCallRejected(notify: { callID: string; usrExtDat?: string }) {
// 提示对方拒绝
}
// 对方挂断了呼叫
notifyCallHangup(notify: { callID: string; usrExtDat?: string }) {
// 呼叫已挂断
}
}
const listener = new CallListener();
listener.addCrNotifierListener([
NOTIFIER_EVENT.notifyCallIn,
NOTIFIER_EVENT.notifyCallAccepted,
NOTIFIER_EVENT.notifyCallRejected,
NOTIFIER_EVENT.notifyCallHangup,
]);
主叫
1.创建房间
- 调用接口:
import { CrSDK } from '@cr/react-native-rtc';
// 创建房间
const meetInfo = await CrSDK.instance.createMeeting();
if (meetInfo.sdkErr === 0) {
// 创建房间成功
} else {
// 创建房间失败
}
2.发起呼叫
注意:
当用户 A 呼叫用户 B 时,只有 B 成功登录了,才可以收到被呼叫通知
- 调用接口:
// A发起呼叫,邀请用户B进入房间
const callResult = await CrSDK.instance.call(userID, meetInfo);
呼叫他人相关 API 请参考:
3.接受/拒绝呼叫
- 回调通知:
在 CallListener 中处理:
// 当 B 接受呼叫时,A 会收到 notifyCallAccepted
// 当 B 拒绝呼叫时,A 会收到 notifyCallRejected
呼叫者相关 API 请参考:
4.挂断
- 调用接口:
// 挂断呼叫
const result = await CrSDK.instance.hangupCall(callID, usrExtDat);
- 回调通知:
// 在 CallListener 中处理 notifyCallHangup
相关 API 请参考:
被叫
1.被呼叫
- 回调通知:
// 在 CallListener 中处理 notifyCallIn
class CallListener extends CrSDKNotifier {
notifyCallIn(notify: { callID: string; callerID: string; usrExtDat?: string }) {
// 通知有呼叫到来
// 可选择接受或拒绝
// 接受呼叫
// const result = await CrSDK.instance.acceptCall(callID);
// 拒绝呼叫
// const result = await CrSDK.instance.rejectCall(callID);
}
}
相关 API 请参考:
2.免打扰
如果用户当前不希望被呼叫,可以把自己的状态设置为免打扰,注意在免打扰状态下不会被呼叫,但是可以主动发起呼叫。
- 调用接口:
const result = await CrSDK.instance.setDNDStatus(1); // 开启免打扰
const result2 = await CrSDK.instance.setDNDStatus(0); // 关闭免打扰
if (result.sdkErr === 0) {
// 设置免打扰成功
} else {
// 设置免打扰失败
}
相关 API 请参考: