邀请机制

更新时间: 2024/08/29 16:28:39

云信信令频道房间可通过主动加入或者接受邀请加入两种方式。

本文主要介绍信令频道房间的邀请机制。

概述

信令频道房间的所有人都有权限邀请他人加入所在的频道房间。

被邀请者可以接受邀请加入频道房间,也可以拒绝邀请。

在被邀请者处理邀请前(接受或拒绝),邀请者也可以取消已发出的邀请。

前提条件

  • 已集成信令 SDK。

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

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

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

使用限制

发出的邀请有效时间为 90 秒,即邀请发出后,如果接受者在 90 秒内没有做出处理,那么该邀请将无效。

实现方法

注册信令服务相关监听

调用 addSignallingListeneron("EventName") 方法注册信令服务相关监听。

注册成功后,当对应的信令事件发生时,SDK 会触发相关回调通知。

  • 建议在初始化后调用该方法。
  • 全局只需注册一次。
  • 该方法为同步。

信令事件回调包括以下四种:

  • onOnlineEvent:在线状态事件回调,返回在线事件对象,包括关闭房间、加入房间、离开房间、邀请加入房间、取消邀请加入房间、拒绝加入邀请、接受加入邀请、控制事件。当本地端或多端同步在线事件成功时会触发该回调。
  • onOfflineEvent:离线状态事件回调,返回离线事件对象,包括关闭房间、加入房间、离开房间、邀请加入房间、取消邀请加入房间、拒绝加入邀请、接受加入邀请、控制事件。当本地端或多端同步离线事件成功时会触发该回调。
  • onMultiClientEvent:多端事件操作回调,返回多端操作的事件对象,包括拒绝加入邀请、接受加入邀请。当多端操作事件成功时会触发该回调。
  • onSyncRoomInfoList:登录后同步信令频道房间列表回调。返回当前未退出的信令频道房间列表。

示例代码如下:

Android
javaNIMClient.getService(V2NIMSignallingService.class).addSignallingListener(listener);
iOS
objective-c@interface Clazz:NSObject<V2NIMSignallingListener>
- (void)addSignallingListener;
- (void)removeSignallingListener;
@end

@implementation Clazz
- (void)addSignallingListener
{
    [[NIMSDK sharedSDK].v2SignallingService addSignallingListener:self];
}

- (void)removeSignallingListener
{
    [[NIMSDK sharedSDK].v2SignallingService removeSignallingListener:self];
}

- (void)onMultiClientEvent:(nonnull V2NIMSignallingEvent *)event {
    // Your Code
}

- (void)onOfflineEvent:(nonnull NSArray<V2NIMSignallingEvent *> *)event { 
    // Your Code
}

- (void)onOnlineEvent:(nonnull V2NIMSignallingEvent *)event { 
    // Your Code
}

- (void)onSyncRoomInfoList:(nonnull NSArray<V2NIMSignallingRoomInfo *> *)channelRooms { 
    // Your Code
}

@end
Windows/macOS
cppsignallingService.addSignallingListener(listener);
Web/uni-app/小程序
typescriptnim.V2NIMSignallingService.on("onOnlineEvent", function (event: V2NIMSignallingEvent) {})
nim.V2NIMSignallingService.on("onOfflineEvent", function (event: V2NIMSignallingEvent[]]) {})
nim.V2NIMSignallingService.on("onMultiClientEvent", function (event: V2NIMSignallingEvent) {})
nim.V2NIMSignallingService.on("onSyncRoomInfoList", function (roomInfoList: V2NIMSignallingRoomInfo[]) {})
HarmonyOS
typescriptsignallingService.on("onOnlineEvent", function (event: V2NIMSignallingEvent) {})
signallingService.on("onOfflineEvent", function (event: V2NIMSignallingEvent[]]) {})
signallingService.on("onMultiClientEvent", function (event: V2NIMSignallingEvent) {})
signallingService.on("onSyncRoomInfoList", function (roomInfoList: V2NIMSignallingRoomInfo[]) {})

如需要取消监听,可以调用 removeSignallingListeneroff("EventName") 方法实现。

示例代码如下:

Android
javaNIMClient.getService(V2NIMSignallingService.class).removeSignallingListener(listener);
iOS
objective-c@interface Clazz:NSObject<V2NIMSignallingListener>
- (void)addSignallingListener;
- (void)removeSignallingListener;
@end

@implementation Clazz
- (void)addSignallingListener
{
    [[NIMSDK sharedSDK].v2SignallingService addSignallingListener:self];
}

- (void)removeSignallingListener
{
    [[NIMSDK sharedSDK].v2SignallingService removeSignallingListener:self];
}

- (void)onMultiClientEvent:(nonnull V2NIMSignallingEvent *)event {
    // Your Code
}

- (void)onOfflineEvent:(nonnull NSArray<V2NIMSignallingEvent *> *)event { 
    // Your Code
}

- (void)onOnlineEvent:(nonnull V2NIMSignallingEvent *)event { 
    // Your Code
}

- (void)onSyncRoomInfoList:(nonnull NSArray<V2NIMSignallingRoomInfo *> *)channelRooms { 
    // Your Code
}

@end
Windows/macOS
cppsignallingService.removeSignallingListener(listener);
Web/uni-app/小程序
typescriptnim.V2NIMSignallingService.off("onOnlineEvent")
nim.V2NIMSignallingService.off("onOfflineEvent")
nim.V2NIMSignallingService.off("onMultiClientEvent")
nim.V2NIMSignallingService.off("onSyncRoomInfoList")
HarmonyOS
typescriptsignallingService.off("onOnlineEvent", function (event: V2NIMSignallingEvent) {})
signallingService.off("onOfflineEvent", function (event: V2NIMSignallingEvent[]]) {})
signallingService.off("onMultiClientEvent", function (event: V2NIMSignallingEvent) {})
signallingService.off("onSyncRoomInfoList", function (roomInfoList: V2NIMSignallingRoomInfo[]) {})

邀请加入信令频道房间

调用 invite 方法邀请他人加入信令频道房间。

信令频道房间内的所有成员都可以邀请他人进入频道房间。

发出邀请后,被邀请者会收到邀请通知。

邀请方在邀请时可以配置是否向被邀请方发送推送消息,默认不推送。如果选择推送,但未配置推送相关信息,则服务器将默认发送以下信息:

  • 房间频道类型为音频:xx邀请你进行语音通话
  • 房间频道类型为视频:xx邀请你进行视频通话
  • 房间频道类型为自定义 xx邀请你进行音视频通话

示例代码如下:

Android
javaString channelId = "channelId";
//被邀请者账号ID
String inviteeAccountId = "inviteeAccountId";
//请求ID
String requestId = "123456";
//服务端扩展字段
String serverExtension = "serverExtension";
//推送配置
V2NIMSignallingPushConfig v2NIMSignallingPushConfig = new V2NIMSignallingPushConfig();
//信令配置
V2NIMSignallingConfig v2NIMSignallingConfig = new V2NIMSignallingConfig();

V2NIMSignallingInviteParams params = new V2NIMSignallingInviteParams.Builder(channelId, inviteeAccountId, requestId)
    .serverExtension("serverExtension")
    .signallingConfig(new V2NIMSignallingConfig())
    .pushConfig(new V2NIMSignallingPushConfig())
    .build();

NIMClient.getService(V2NIMSignallingService.class).invite(params, new V2NIMSuccessCallback<Void>() {
    @Override
    public void onSuccess(Void unused) {

    }
}, new V2NIMFailureCallback() {
    @Override
    public void onFailure(V2NIMError error) {
        
    }
});
iOS
objective-cV2NIMSignallingInviteParams * param = [[V2NIMSignallingInviteParams alloc] init];
param.channelId = @"Channel ID";
// 被邀请者账号ID,不能填自己的账号ID
param.inviteeAccountId = @"Account ID";
param.requestId = @"Request ID";
param.serverExtension = @"{\"key\": \"value\"}";
[[NIMSDK sharedSDK].v2SignallingService invite:param success:^() {
    // Your Code
} failure:^(V2NIMError *error) {
    // Your Code
}];
Windows/macOS
cppV2NIMSignallingInviteParams inviteParameter;
inviteParameter.channelId = "channelId";
inviteParameter.accountId = "accountId";
inviteParameter.requestId = "request UUID";
signallingService.invite(
    inviteParameter,
    []() {
        // invite success
    },
    [](V2NIMError error) {
        // invite failed, handle error
    });
Web/uni-app/小程序
typescriptconst params: V2NIMSignallingInviteParams = {
  channelId: this.channelId,
  inviteeAccountId: this.inviteeAccountId,
  requestId: this.requestId,
  serverExtension: this.serverExtension,
  pushConfig: {
    pushEnabled: this.pushEnabled,
    pushTitle: this.pushTitle,
    pushContent: this.pushContent,
    pushPayload: this.pushPayload
  },
  signallingConfig: {
    offlineEnabled: this.offlineEnabled,
    unreadEnabled: this.unreadEnabled,
    selfUid: this.selfUid
  }
}

await nimsdk.V2NIMSignallingService.invite(params)
HarmonyOS
typescriptconst params: V2NIMSignallingInviteParams = {
  channelId: this.channelId,
  inviteeAccountId: this.inviteeAccountId,
  requestId: this.requestId,
  serverExtension: this.serverExtension,
  pushConfig: {
    pushEnabled: this.pushEnabled,
    pushTitle: this.pushTitle,
    pushContent: this.pushContent,
    pushPayload: this.pushPayload
  },
  signallingConfig: {
    offlineEnabled: this.offlineEnabled,
    unreadEnabled: this.unreadEnabled,
    selfUid: this.selfUid
  }
}

await nimsdk.signallingService.invite(params)

接受邀请

调用 acceptInvite 方法接受他人的入房邀请,即加入信令频道房间。

接受邀请加入频道房间后,邀请人会收到被邀请者已接受邀请的通知。

被邀请者需要在邀请失效(邀请的有效期为 90 秒)前接受邀请,才能加入信令频道房间。


示例代码如下:

Android
javaString channelId = "channelId";
//被邀请者账号ID
String inviterAccountId = "inviterAccountId";
//请求ID
String requestId = "123456";
//服务端扩展字段
String serverExtension = "serverExtension";
//是否需要存离线消息。如果存离线,则用户离线再上线会收到该通知
boolean offlineEnabled = true;

V2NIMSignallingAcceptInviteParams params = new V2NIMSignallingAcceptInviteParams.Builder(channelId, inviterAccountId, requestId)
    .serverExtension(serverExtension)
    .offlineEnabled(offlineEnabled)
    .build();
NIMClient.getService(V2NIMSignallingService.class).acceptInvite(params, new V2NIMSuccessCallback<Void>() {
    @Override
    public void onSuccess(Void unused) {

    }
}, new V2NIMFailureCallback() {
    @Override
    public void onFailure(V2NIMError error) {

    }
});
iOS
objective-cV2NIMSignallingAcceptInviteParams* param = [[V2NIMSignallingAcceptInviteParams alloc] init];
param.channelId = @"Channel ID";
// 邀请者账号ID,不能填自己的账号ID
param.inviterAccountId = @"Account ID";
param.requestId = @"Request ID";
param.serverExtension = @"{\"key\": \"value\"}";
param.offlineEnabled = YES;

[[NIMSDK sharedSDK].v2SignallingService acceptInvite:param success:^() {
    // Your Code
} failure:^(V2NIMError *error) {
    // Your Code
}];
Windows/macOS
cppV2NIMSignallingAcceptInviteParams acceptInviteParameter;
acceptInviteParameter.channelId = "channelId";
acceptInviteParameter.accountId = "accountId";
acceptInviteParameter.requestId = "request UUID";
signallingService.acceptInvite(
    acceptInviteParameter,
    []() {
        // accept invite success
    },
    [](V2NIMError error) {
        // accept invite failed, handle error
    });
Web/uni-app/小程序
typescriptconst params: V2NIMSignallingAcceptInviteParams = {
  channelId: this.channelId,
  inviterAccountId: this.inviterAccountId,
  requestId: this.requestId,
  serverExtension: this.serverExtension,
  offlineEnabled: this.offlineEnabled
}

await nimsdk.V2NIMSignallingService.acceptInvite(params)
HarmonyOS
typescriptconst params: V2NIMSignallingAcceptInviteParams = {
  channelId: this.channelId,
  inviterAccountId: this.inviterAccountId,
  requestId: this.requestId,
  serverExtension: this.serverExtension,
  offlineEnabled: this.offlineEnabled
}

await nimsdk.signallingService.acceptInvite(params)

拒绝邀请

调用 rejectInvite 方法拒绝他人的入房邀请,即不加入信令频道房间。

拒绝邀请加入频道房间后,邀请人会收到被邀请者已拒绝邀请的通知。

被邀请者未在有效期内(90 秒)处理该邀请,即视为拒绝加入信令频道房间。


示例代码如下:

Android
javaString channelId = "channelId";
//被邀请者账号ID
String inviteeAccountId = "inviteeAccountId";
//请求ID
String requestId = "123456";
//服务端扩展字段
String serverExtension = "serverExtension";
//是否需要存离线消息。如果存离线,则用户离线再上线会收到该通知
boolean offlineEnabled = true;

V2NIMSignallingRejectInviteParams params = new V2NIMSignallingRejectInviteParams.Builder(channelId, inviteeAccountId, requestId)
    .serverExtension(serverExtension)
    .offlineEnabled(offlineEnabled)
    .build();

NIMClient.getService(V2NIMSignallingService.class).rejectInvite(params, new V2NIMSuccessCallback<Void>() {
    @Override
    public void onSuccess(Void unused) {

    }
}, new V2NIMFailureCallback() {
    @Override
    public void onFailure(V2NIMError error) {

    }
});
iOS
objective-cV2NIMSignallingRejectInviteParams* param = [[V2NIMSignallingRejectInviteParams alloc] init];
param.channelId = @"Channel ID";
// 邀请者账号ID,不能填自己的账号ID
param.inviterAccountId = @"Account ID";
param.requestId = @"Request ID";
param.serverExtension = @"{\"key\": \"value\"}";
[[NIMSDK sharedSDK].v2SignallingService rejectInvite:param success:^() {
    // Your Code
} failure:^(V2NIMError *error) {
    // Your Code
}];
Windows/macOS
cppV2NIMSignallingRejectInviteParams rejectInviteParameter;
rejectInviteParameter.channelId = "channelId";
rejectInviteParameter.accountId = "accountId";
rejectInviteParameter.requestId = "request UUID";
signallingService.rejectInvite(
    rejectInviteParameter,
    []() {
        // reject invite success
    },
    [](V2NIMError error) {
        // reject invite failed, handle error
    });
Web/uni-app/小程序
typescriptconst params: V2NIMSignallingRejectInviteParams = {
  channelId: this.channelId,
  inviterAccountId: this.inviterAccountId,
  requestId: this.requestId,
  serverExtension: this.serverExtension,
  offlineEnabled: this.offlineEnabled
}

await nimsdk.V2NIMSignallingService.rejectInvite(params)
HarmonyOS
typescriptconst params: V2NIMSignallingRejectInviteParams = {
  channelId: this.channelId,
  inviterAccountId: this.inviterAccountId,
  requestId: this.requestId,
  serverExtension: this.serverExtension,
  offlineEnabled: this.offlineEnabled
}

await nimsdk.signallingService.rejectInvite(params)

取消邀请

调用 cancelInvite 方法取消已发布的入房邀请。

信令频道房间内的用户邀请他人进入信令频道房间后,在被邀请者处理该邀请前(接受或拒绝),可以取消已发出的入房邀请。取消后,被邀请的用户会收到取消邀请的通知。

  • 只能取消自己发出的邀请,无法影响房间内其他用户发出的邀请。

  • 邀请的有效期为 90 秒。即发出邀请后超过 90 秒或者被邀请者已处理,该邀请将变为无效,发起方无需再取消邀请。


示例代码如下:

Android
javaString channelId = "channelId";
//被邀请者账号ID
String inviteeAccountId = "inviteeAccountId";
//请求ID
String requestId = "123456";
//服务端扩展字段
String serverExtension = "serverExtension";
//是否需要存离线消息。如果存离线,则用户离线再上线会收到该通知
boolean offlineEnabled = true;

V2NIMSignallingCancelInviteParams params = new V2NIMSignallingCancelInviteParams.Builder(channelId,inviteeAccountId,requestId)
    .serverExtension("serverExtension")
    .offlineEnabled(offlineEnabled)
    .build();
NIMClient.getService(V2NIMSignallingService.class).cancelInvite(params, new V2NIMSuccessCallback<Void>() {
    @Override
    public void onSuccess(Void unused) {

    }
}, new V2NIMFailureCallback() {
    @Override
    public void onFailure(V2NIMError error) {
        
    }
});
iOS
objective-cV2NIMSignallingCancelInviteParams* param = [[V2NIMSignallingCancelInviteParams alloc] init];
param.channelId = @"Channel ID";
// 被邀请者账号ID,不能填自己的账号ID
param.inviteeAccountId = @"Account ID";
param.requestId = @"Request ID";
param.serverExtension = @"{\"key\": \"value\"}";
param.offlineEnabled = YES;

[[NIMSDK sharedSDK].v2SignallingService cancelInvite:param success:^() {
    // Your Code
} failure:^(V2NIMError *error) {
    // Your Code
}];
Windows/macOS
cppV2NIMSignallingCancelInviteParams cancelInviteParameter;
cancelInviteParameter.channelId = "channelId";
cancelInviteParameter.accountId = "accountId";
cancelInviteParameter.requestId = "request UUID";
signallingService.cancelInvite(
    cancelInviteParameter,
    []() {
        // cancel invite success
    },
    [](V2NIMError error) {
        // cancel invite failed, handle error
    });
Web/uni-app/小程序
typescriptconst params: V2NIMSignallingCancelInviteParams = {
  channelId: this.channelId,
  inviteeAccountId: this.inviteeAccountId,
  requestId: this.requestId,
  serverExtension: this.serverExtension,
  offlineEnabled: this.offlineEnabled
}

await nimsdk.V2NIMSignallingService.cancelInvite(params)
HarmonyOS
typescriptconst params: V2NIMSignallingCancelInviteParams = {
  channelId: this.channelId,
  inviteeAccountId: this.inviteeAccountId,
  requestId: this.requestId,
  serverExtension: this.serverExtension,
  offlineEnabled: this.offlineEnabled
}

await nimsdk.signallingService.cancelInvite(params)
此文档是否对你有帮助?
有帮助
去反馈
  • 概述
  • 前提条件
  • 使用限制
  • 实现方法
  • 注册信令服务相关监听
  • 邀请加入信令频道房间
  • 接受邀请
  • 拒绝邀请
  • 取消邀请