频道房间管理
更新时间: 2024/08/29 16:23:57
概述
信令是指在通信系统中用于建立、维护和结束通信会话的控制信息。它包含了传输数据所需的必要信息,比如呼叫请求、呼叫接受、呼叫拒绝、呼叫挂断等。
信令在通信系统中起着调度和控制的作用,确保通信的顺利进行。
信令频道是信令的载体,是建立呼叫的基础。
在云信底层实现中,通过房间来对应频道,即频道与房间一一对应,可直接视为同一含义,下文统一用信令频道房间表示。
SDK 的 V2NIMSignallingService
类提供管理频道房间的方法,包括创建、加入、离开、关闭。
前提条件
-
已集成信令 SDK。
信令 SDK 包含在 IM 即时通讯 SDK 中,集成请参考 集成 SDK。
-
已完成信令 SDK 的初始化,具体请参考 初始化 SDK。
-
已登录 IM 账号,具体请参考 登录 IM。
使用限制
每个信令频道房间中的人数默认上限为 100。
实现方法
注册信令服务相关监听
调用 addSignallingListener
或 on("EventName")
方法注册信令服务相关监听。
注册成功后,当对应的信令事件发生时,SDK 会触发相关回调通知。
- 建议在初始化后调用该方法。
- 全局只需注册一次。
- 该方法为同步。
信令事件回调包括以下四种:
onOnlineEvent
:在线状态事件回调,返回在线事件对象,包括关闭房间、加入房间、离开房间、邀请加入房间、取消邀请加入房间、拒绝加入邀请、接受加入邀请、控制事件。当本地端或多端同步在线事件成功时会触发该回调。onOfflineEvent
:离线状态事件回调,返回离线事件对象,包括关闭房间、加入房间、离开房间、邀请加入房间、取消邀请加入房间、拒绝加入邀请、接受加入邀请、控制事件。当本地端或多端同步离线事件成功时会触发该回调。onMultiClientEvent
:多端事件操作回调,返回多端操作的事件对象,包括拒绝加入邀请、接受加入邀请。当多端操作事件成功时会触发该回调。onSyncRoomInfoList
:登录后同步信令频道房间列表回调。返回当前未退出的信令频道房间列表。
示例代码如下:
javaNIMClient.getService(V2NIMSignallingService.class).addSignallingListener(listener);
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
cppsignallingService.addSignallingListener(listener);
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[]) {})
typescriptsignallingService.on("onOnlineEvent", function (event: V2NIMSignallingEvent) {})
signallingService.on("onOfflineEvent", function (event: V2NIMSignallingEvent[]]) {})
signallingService.on("onMultiClientEvent", function (event: V2NIMSignallingEvent) {})
signallingService.on("onSyncRoomInfoList", function (roomInfoList: V2NIMSignallingRoomInfo[]) {})
如需要取消监听,可以调用 removeSignallingListener
或 off("EventName")
方法实现。
示例代码如下:
javaNIMClient.getService(V2NIMSignallingService.class).removeSignallingListener(listener);
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
cppsignallingService.removeSignallingListener(listener);
typescriptnim.V2NIMSignallingService.off("onOnlineEvent")
nim.V2NIMSignallingService.off("onOfflineEvent")
nim.V2NIMSignallingService.off("onMultiClientEvent")
nim.V2NIMSignallingService.off("onSyncRoomInfoList")
typescriptsignallingService.off("onOnlineEvent", function (event: V2NIMSignallingEvent) {})
signallingService.off("onOfflineEvent", function (event: V2NIMSignallingEvent[]]) {})
signallingService.off("onMultiClientEvent", function (event: V2NIMSignallingEvent) {})
signallingService.off("onSyncRoomInfoList", function (roomInfoList: V2NIMSignallingRoomInfo[]) {})
创建信令频道房间
调用 createRoom
方法创建信令频道房间。
云信信令支持创建三种类型的频道房间:音频频道房间、视频频道房间以及自定义频道房间。
频道房间创建后,默认的有效时间为 2 小时。如果房间内的用户都没有主动关闭信令频道房间,那么服务器默认在会 2 小时后销毁该信令频道房间(2 小时内没有新用户加入房间,如有,从加入后再开始计算)。
- 在服务器中,频道(房间)名称存在唯一性,即相同的频道(房间)名称,在服务器同时只能存在一个。
- 每个频道房间中的人数默认上限为 100。
- 创建频道房间后并不会主动加入频道房间,需要手动加入频道房间。
示例代码如下:
java//频道类型
V2NIMSignallingChannelType channelType = V2NIMSignallingChannelType.V2NIM_SIGNALLING_CHANNEL_TYPE_VIDEO;
//频道名称
String channelName = "channelName";
//频道扩展字段
String channelExtension = "channelExtension";
NIMClient.getService(V2NIMSignallingService.class).createRoom(channelType, channelName, channelExtension, new V2NIMSuccessCallback<V2NIMSignallingChannelInfo>() {
@Override
public void onSuccess(V2NIMSignallingChannelInfo v2NIMSignallingChannelInfo) {
//create room success
}
}, new V2NIMFailureCallback() {
@Override
public void onFailure(V2NIMError error) {
//create room failure
}
});
objective-c// 创建视频频道
V2NIMSignallingChannelType channelType = V2NIM_SIGNALLING_CHANNEL_TYPE_VIDEO;
NSString *channelName = @"Channel Name";
NSString *channelExtension = @"{\"key\": \"value\"}";
[[NIMSDK sharedSDK].v2SignallingService createRoom:channelType channelName:channelName channelExtension:channelExtension success:^(V2NIMSignallingChannelInfo *result) {
// Your Code
} failure:^(V2NIMError *error) {
// Your Code
}];
cppsignallingService.createRoom(
V2NIM_SIGNALLING_CHANNEL_TYPE_AUDIO,
"channelName",
"{\"key\":\"value\"}",
[](const V2NIMSignallingChannelInfo& result) {
// create room success
},
[](V2NIMError error) {
// create room failed, handle error
});
typescriptconst result:V2NIMSignallingChannelInfo = await nimsdk.V2NIMSignallingService.createRoom(this.channelType,
this.channelName, this.channelExt)
typescriptconst result:V2NIMSignallingChannelInfo = await nimsdk.signallingService.createRoom(this.channelType,
this.channelName, this.channelExt)
加入信令频道房间
调用 joinRoom
方法加入信令频道房间。
加入频道房间后,房间内的所有人都会收到有用户加入的通知消息。
示例代码如下:
javaString channelId = "channelId";
//信令配置
V2NIMSignallingConfig v2NIMSignallingConfig = new V2NIMSignallingConfig();
//音视频配置
V2NIMSignallingRtcConfig v2NIMSignallingRtcConfig = new V2NIMSignallingRtcConfig("rtcChannelName",60 * 60L,null);
V2NIMSignallingJoinParams params = new V2NIMSignallingJoinParams.Builder(channelId)
.serverExtension("serverExtension")
.signallingConfig(v2NIMSignallingConfig)
.rtcConfig(v2NIMSignallingRtcConfig)
.build();
NIMClient.getService(V2NIMSignallingService.class).joinRoom(params, new V2NIMSuccessCallback<V2NIMSignallingRoomInfo>() {
@Override
public void onSuccess(V2NIMSignallingRoomInfo v2NIMSignallingRoomInfo) {
//join room success
}
}, new V2NIMFailureCallback() {
@Override
public void onFailure(V2NIMError error) {
//join room failure
}
});
objective-cV2NIMSignallingJoinParams * param = [[V2NIMSignallingJoinParams alloc] init];
param.channelId = @"Channel ID";
param.serverExtension = @"{\"key\": \"value\"}";
[[NIMSDK sharedSDK].v2SignallingService joinRoom:param success:^(V2NIMSignallingRoomInfo *result) {
// Your Code
} failure:^(V2NIMError *error) {
// Your Code
}];
cppV2NIMSignallingJoinParams joinParameter;
joinParams.channelId = "channelId";
signallingService.joinRoom(
joinParameter,
[](const V2NIMSignallingChannelInfo& result) {
// join room success
},
[](V2NIMError error) {
// join room failed, handle error
});
typescriptconst signallingConfig = {
offlineEnabled : this.offlineEnabled,
unreadEnabled: this.unreadEnabled,
selfUid: this.selfUid
} as V2NIMSignallingConfig
const rtcConfig= {
rtcChannelName: this.rtcChannelName,
rtcTokenTtl: this.rtcTokenTtl,
rtcParams: this.rtcParams
} as V2NIMSignallingRtcConfig
const params: V2NIMSignallingJoinParams = {
channelId: this.channelId,
serverExtension: this.serverExtension,
signallingConfig: signallingConfig,
rtcConfig: rtcConfig
}
const result:V2NIMSignallingRoomResult = await nimsdk.V2NIMSignallingService.joinRoom(params)
typescriptconst signallingConfig = {
offlineEnabled : this.offlineEnabled,
unreadEnabled: this.unreadEnabled,
selfUid: this.selfUid
} as V2NIMSignallingConfig
const rtcConfig= {
rtcChannelName: this.rtcChannelName,
rtcTokenTtl: this.rtcTokenTtl,
rtcParams: this.rtcParams
} as V2NIMSignallingRtcConfig
const params: V2NIMSignallingJoinParams = {
channelId: this.channelId,
serverExtension: this.serverExtension,
signallingConfig: signallingConfig,
rtcConfig: rtcConfig
}
const result:V2NIMSignallingRoomResult = await nimsdk.signallingService.joinRoom(params)
离开信令频道房间
调用 leaveRoom
方法离开信令频道房间。
离开频道房间后,房间内的所有人都会收到有用户离开的通知消息。
示例代码如下:
javaString channelId = "channelId";
//是否需要存离线消息。如果存离线,则用户离线再上线会收到该通知
boolean offlineEnabled = true;
//服务端扩展字段, 长度限制4096
String serverExtension = "serverExtension";
NIMClient.getService(V2NIMSignallingService.class).leaveRoom(channelId,offlineEnabled,serverExtension, new V2NIMSuccessCallback<Void>() {
@Override
public void onSuccess(Void aVoid) {
//leave room success
}
}, new V2NIMFailureCallback() {
@Override
public void onFailure(V2NIMError error) {
//leave room failure
}
});
objective-cNSString * channelId = @"Channel ID";
BOOL offlineEnabled = YES;
NSString *serverExtension = @"{\"key\": \"value\"}";
[[NIMSDK sharedSDK].v2SignallingService leaveRoom:channelId offlineEnabled:offlineEnabled serverExtension:serverExtension success:^() {
// Your Code
} failure:^(V2NIMError *error) {
// Your Code
}];
cppsignallingService.leaveRoom(
"channelId",
true,
"{\"key\":\"value\"}",
[]() {
// leave room success
},
[](V2NIMError error) {
// leave room failed, handle error
});
typescriptawait nimsdk.V2NIMSignallingService.leaveRoom(this.channelId, this.offlineEnabled, this.serverExtension)
typescriptawait nimsdk.signallingService.leaveRoom(this.channelId, this.offlineEnabled, this.serverExtension)
关闭信令频道房间
调用 closeRoom
方法关闭信令频道房间。
房间内的所有人都可以关闭信令频道房间。关闭后,房间内的所有人都会收到房间被关闭的通知。
如果房间内的用户都没有主动关闭信令频道房间,那么服务器默认在会 2 小时后销毁该信令频道房间(2 小时内没有新用户加入房间,如有,从加入后再开始计算)。
示例代码如下:
javaString channelId = "channelId";
//是否需要存离线消息。如果存离线,则用户离线再上线会收到该通知
boolean offlineEnabled = true;
//服务端扩展字段, 长度限制4096
String serverExtension = "serverExtension";
NIMClient.getService(V2NIMSignallingService.class).closeRoom(channelId, true, "serverExtension", new V2NIMSuccessCallback<Void>() {
@Override
public void onSuccess(Void aVoid) {
//close room success
}
}, new V2NIMFailureCallback() {
@Override
public void onFailure(V2NIMError error) {
//close room failure
}
});
objective-cNSString * channelId = @"Channel ID";
BOOL offlineEnabled = YES;
NSString *serverExtension = @"{\"key\": \"value\"}";
[[NIMSDK sharedSDK].v2SignallingService closeRoom:channelId offlineEnabled:offlineEnabled serverExtension:serverExtension success:^() {
// Your Code
} failure:^(V2NIMError *error) {
// Your Code
}];
cppsignallingService.closeRoom(
"channelId",
true,
"{\"key\":\"value\"}",
[]() {
// close room success
},
[](V2NIMError error) {
// close room failed, handle error
});
typescriptconst channelId = 'XXX_Id'
const offlineEnabled = false
const serverExtension = ''
await nimsdk.V2NIMSignallingService.closeRoom(channelId, offlineEnabled, serverExtension)
typescriptconst channelId = 'XXX_Id'
const offlineEnabled = false
const serverExtension = ''
await nimsdk.signallingService.closeRoom(channelId, offlineEnabled, serverExtension)
查询信令频道房间信息
调用 getRoomInfoByChannelName
方法根据频道名称查询信令频道房间信息。
示例代码如下:
javaNIMClient.getService(V2NIMSignallingService.class).getRoomInfoByChannelName("channelName", new V2NIMSuccessCallback<V2NIMSignallingRoomInfo>() {
@Override
public void onSuccess(V2NIMSignallingRoomInfo v2NIMSignallingRoomInfo) {
//get room info success
}
}, new V2NIMFailureCallback() {
@Override
public void onFailure(V2NIMError error) {
//get room info failure
}
});
objective-cNSString * channelName = @"Channel Name";
[[NIMSDK sharedSDK].v2SignallingService getRoomInfoByChannelName:channelName success:^(V2NIMSignallingRoomInfo *result) {
// Your Code
} failure:^(V2NIMError *error) {
// Your Code
}];
cppsignallingService.getRoomInfoByChannelName(
"channelName",
[](const V2NIMSignallingRoomInfo& result) {
// get room info success
},
[](V2NIMError error) {
// get room info failed, handle error
});
typescriptconst result = await nim.V2NIMSignallingService.getRoomInfoByChannelName("YOUR_CHANNEL_NAME")
typescriptconst result:V2NIMSignallingRoomResult = await nimsdk.signallingService.getRoomInfoByChannelName(this.channelName)