实现呼叫功能

更新时间: 2024/08/29 16:19:42

本文主要介绍信令 SDK 呼叫功能的实现。

前提条件

  • 已集成信令 SDK。

    信令 SDK 包含在 IM 即时通讯 SDK 中,集成请参考 集成 SDK

  • 已完成信令 SDK 的初始化,具体请参考 初始化 SDK

  • 已登录 IM 账号,具体请参考 登录 IM

实现步骤

建立呼叫的正常流程如下:

呼叫发起方:


  flowchart LR
    classDef default fill:#337EFF,stroke:#337EFF,stroke-width:0px,color:#FFFFFF;
    
    A("创建信令频道房间(createRoom)") --> B("自己加入房间(joinRoom)") --> C("邀请对方加入房间(invite)")
    click A "https://doc.yunxin.163.com/signaling/client-apis/jE4MDI2OTM?platform=client#createRoom"
    click B "https://doc.yunxin.163.com/signaling/client-apis/jE4MDI2OTM?platform=client#joinRoom"
    click C "https://doc.yunxin.163.com/signaling/client-apis/jE4MDI2OTM?platform=client#invite"

呼叫接收方:


  flowchart LR
    classDef default fill:#337EFF,stroke:#337EFF,stroke-width:0px,color:#FFFFFF;
    
    A("加入信令频道房间(joinRoom)") --> B("接受对方的呼叫(acceptInvite)")
    click A "https://doc.yunxin.163.com/signaling/client-apis/jE4MDI2OTM?platform=client#joinRoom"
    click B "https://doc.yunxin.163.com/signaling/client-apis/jE4MDI2OTM?platform=client#acceptInvite"

为了加速呼叫流程,云信信令通过将几个接口进行组合封装。

  1. 发起方调用 call 方法发起呼叫。

  2. 接收调用 callSetup接通呼叫。


  flowchart LR
    classDef default fill:#337EFF,stroke:#337EFF,stroke-width:0px,color:#FFFFFF;
    
    A("直接发起呼叫(call)") --> B("直接接通呼叫(callSetup)")
    click A "https://doc.yunxin.163.com/signaling/client-apis/jE4MDI2OTM?platform=client#call"
    click B "https://doc.yunxin.163.com/signaling/client-apis/jE4MDI2OTM?platform=client#callSetup"

示例代码如下:

Android
java//发起方

//被呼叫者账号ID不能填自己的账号ID
String calleeAccountId = "test";
//请求ID
String requestId = "123456";
//频道类型
V2NIMSignallingChannelType channelType = V2NIMSignallingChannelType.V2NIM_SIGNALLING_CHANNEL_TYPE_VIDEO;
//推送配置
V2NIMSignallingPushConfig v2NIMSignallingPushConfig = new V2NIMSignallingPushConfig();
//信令配置
V2NIMSignallingConfig v2NIMSignallingConfig = new V2NIMSignallingConfig();
//音视频配置
V2NIMSignallingRtcConfig v2NIMSignallingRtcConfig = new V2NIMSignallingRtcConfig("rtcChannelName",60 * 60L,null);

V2NIMSignallingCallParams params = new V2NIMSignallingCallParams.Builder(calleeAccountId, requestId, channelType)
    .channelExtension("channelExtension")
    .channelName("channelName")
    .serverExtension("serverExtension")
    .pushConfig(v2NIMSignallingPushConfig)
    .signallingConfig(v2NIMSignallingConfig)
    .rtcConfig(v2NIMSignallingRtcConfig)
    .build();
NIMClient.getService(V2NIMSignallingService.class).call(params, new V2NIMSuccessCallback<V2NIMSignallingCallResult>() {
    @Override
    public void onSuccess(V2NIMSignallingCallResult v2NIMSignallingCallResult) {
        //call success
    }
}, new V2NIMFailureCallback() {
    @Override
    public void onFailure(V2NIMError error) {
        //call failure
    }
});

//接受方

String channelId = "channelId";
//被呼叫者账号ID不能填自己的账号ID
String callerAccountId = "caller account id";
//请求ID
String requestId = "123456";
//信令配置
V2NIMSignallingConfig v2NIMSignallingConfig = new V2NIMSignallingConfig();
//音视频配置
V2NIMSignallingRtcConfig v2NIMSignallingRtcConfig = new V2NIMSignallingRtcConfig("rtcChannelName",60 * 60L,null);


V2NIMSignallingCallSetupParams setupParams = new V2NIMSignallingCallSetupParams.Builder(channelId, callerAccountId, requestId)
    .serverExtension("serverExtension")
    .signallingConfig(v2NIMSignallingConfig)
    .rtcConfig(v2NIMSignallingRtcConfig)
    .build();

NIMClient.getService(V2NIMSignallingService.class).callSetup(setupParams, new V2NIMSuccessCallback<V2NIMSignallingCallSetupResult>() {
    @Override
    public void onSuccess(V2NIMSignallingCallSetupResult v2NIMSignallingCallSetupResult) {
        //call setup success
    }
}, new V2NIMFailureCallback() {
    @Override
    public void onFailure(V2NIMError error) {
        //call setup failure
    }
});
iOS
objective-c//发起方

V2NIMSignallingCallParams* param = [[V2NIMSignallingCallParams alloc] init];
// 被呼叫者账号ID不能填自己的账号ID
param.calleeAccountId = @"Account ID";
param.requestId = @"Request ID";
// 创建音频频道
V.channelType = V2NIM_SIGNALLING_CHANNEL_TYPE_AUDIO;
param.channelName = @"Channel Name";

[[NIMSDK sharedSDK].v2SignallingService call:param success:^(V2NIMSignallingCallResult *result) {
    // Your Code
} failure:^(V2NIMError *error) {
    // Your Code
}];


//接受方
V2NIMSignallingCallSetupParams* param = [[V2NIMSignallingCallSetupParams alloc] init];
param.channelId = @"Channel ID";
// 接受的呼叫者账号ID,不能填自己的账号ID
param.callerAccountId = @"Account ID";
param.requestId = @"Request ID";

[[NIMSDK sharedSDK].v2SignallingService callSetup:param success:^(V2NIMSignallingCallSetupResult *result) {
    // Your Code
} failure:^(V2NIMError *error) {
    // Your Code
}];
Windows/macOS
cpp//发起方

V2NIMSignallingCallParams callParameter;
callParameter.calleeAccountId = "calleeAccountId";
callParameter.requestId = "request UUID";
callParameter.channelType = V2NIM_SIGNALLING_CHANNEL_TYPE_AUDIO;
signallingService.call(
    callParameter,
    [](const V2NIMSignallingCallResult& result) {
        // call success
    },
    [](V2NIMError error) {
        // call failed, handle error
    });


//接受方

V2NIMSignallingCallSetupParams callSetupParams;
callSetupParams.channelId = "channelId";
callSetupParams.callerAccountId = "callerAccountId";
callSetupParams.requestId = "request UUID";
signallingService.callSetup(
    callSetupParams,
    [](const V2NIMSignallingCallSetupResult& result) {
        // call setup success
    },
    [](V2NIMError error) {
        // call setup failed, handle error
    });
Web/uni-app/小程序
typescript//发起方

const params: V2NIMSignallingCallParams = {
  calleeAccountId: this.calleeAccountId,
  requestId: this.requestId,
  channelType: this.channelType,
  channelName: this.channelName,
  channelExtension: this.channelExtension,
  serverExtension: this.serverExtension,
  signallingConfig: {
    unreadEnabled: this.unreadEnabled,
    offlineEnabled: this.offlineEnabled,
    selfUid: this.selfUid
  },
  pushConfig: {
    pushEnabled: this.pushEnabled,
    pushTitle: this.pushTitle,
    pushContent: this.pushContent,
    pushPayload: this.pushPayload
  },
  rtcConfig: {
    rtcChannelName: this.rtcChannelName,
    rtcTokenTtl: this.rtcTokenTtl,
    rtcParams: this.rtcParams
  }
}

const result:V2NIMSignallingCallResult = await nimsdk.V2NIMSignallingService.call(params)

//接受方

const params: V2NIMSignallingCallSetupParams = {
  callerAccountId: this.callerAccountId,
  requestId: this.requestId,
  channelId: this.channelId,
  serverExtension: this.serverExtension,
  signallingConfig: {
    unreadEnabled: this.unreadEnabled,
    offlineEnabled: this.offlineEnabled,
    selfUid: this.selfUid
  },
  rtcConfig: {
    rtcChannelName: this.rtcChannelName,
    rtcTokenTtl: this.rtcTokenTtl,
    rtcParams: this.rtcParams
  }
}

const result:V2NIMSignallingCallSetupResult = await nimsdk.V2NIMSignallingService.callSetup(params)
HarmonyOS
typescript
//发起方
const params: V2NIMSignallingCallParams = {
  calleeAccountId: this.calleeAccountId,
  requestId: this.requestId,
  channelType: this.channelType,
  channelName: this.channelName,
  channelExtension: this.channelExtension,
  serverExtension: this.serverExtension,
  signallingConfig: {
    unreadEnabled: this.unreadEnabled,
    offlineEnabled: this.offlineEnabled,
    selfUid: this.selfUid
  },
  pushConfig: {
    pushEnabled: this.pushEnabled,
    pushTitle: this.pushTitle,
    pushContent: this.pushContent,
    pushPayload: this.pushPayload
  },
  rtcConfig: {
    rtcChannelName: this.rtcChannelName,
    rtcTokenTtl: this.rtcTokenTtl,
    rtcParams: this.rtcParams
  }
}

const result:V2NIMSignallingCallResult = await nimsdk.signallingService.call(params)


//接受方

const params: V2NIMSignallingCallSetupParams = {
  callerAccountId: this.callerAccountId,
  requestId: this.requestId,
  channelId: this.channelId,
  serverExtension: this.serverExtension,
  signallingConfig: {
    unreadEnabled: this.unreadEnabled,
    offlineEnabled: this.offlineEnabled,
    selfUid: this.selfUid
  },
  rtcConfig: {
    rtcChannelName: this.rtcChannelName,
    rtcTokenTtl: this.rtcTokenTtl,
    rtcParams: this.rtcParams
  }
}

const result:V2NIMSignallingCallSetupResult = await nimsdk.signallingService.callSetup(params)
此文档是否对你有帮助?
有帮助
去反馈
  • 前提条件
  • 实现步骤