本文主要介绍进入房间的API以及如何接收房间内其他成员进出房间的消息。
说明
进入房间时需要携带当前用户的昵称,也可以携带与当前用户相关的一些自定义信息,比如头像图片地址等。
某成员进入房间成功后,房间内的所有人(包括自己)都会收到通知。
进入房间API使用示例
iOS端(Objective-C):
//进入房间有两个API可以选择使用
// 可以只输入一个当前用户的昵称,支持任意字符;
1. [id<AIRBRoomChannelProtocol>room enterRoomWithUserNick:@"用户的昵称"];
// 也可以在输入昵称基础上,携带更多的用户的个人信息,比如头像地址等
2. [id<AIRBRoomChannelProtocol>room enterRoomWithUserNick:@"用户的昵称" extension:@{
@"avatar" : @"https://xxxx.png"
}];
// 进入房间成功,需要通过实现AIRBRoomChannelProtocol的AIRBRoomChannelDelegate中的如下方法和事件来通知,
- (void) onAIRBRoomChannelEvent:(AIRBRoomChannelEvent) event info:(NSDictionary*)info{
switch (event) {
case AIRBRoomChannelEventEntered:
break;
.....
}
}
//进入房间失败,需要监听AIRBRoomChannelDelegate中的如下错误事件
- (void) onAIRBRoomChannelErrorWithCode:(AIRBErrorCode)code message:(NSString*)message{
switch (code) {
case AIRBRoomChannelFailedToEnterRoom:
break;
.....
}
}
Android端(Java):
//进入房间有两个API可以选择使用
1. 不带拓展字段
roomChannel.enterRoom(nick, new Callback<Void>() {
@Override
public void onSuccess(Void data) {
// 进房间成功
}
@Override
public void onError(String errorMsg) {
// 进房间失败
}
});
2. 带拓展字段
Map<String, String> extension = new HashMap<>();
extension.put("customKey1", "value1");
extension.put("customKey2", "value2");
roomChannel.enterRoom(nick, extension, new Callback<Void>() {
@Override
public void onSuccess(Void data) {
// 进房间成功
}
@Override
public void onError(String errorMsg) {
// 进房间失败
}
});
Web端(JavaScript):
roomChannel.enterRoom(nick, extension).then((roomDetail) => {
// 在这里进行后续操作
})
// 或者在async函数中使用await
const roomDetail = await roomChannel.enterRoom(nick, extension)
Windows端 (C++):
std::string room_id;
std::string nick_name;
auto room_ptr = alibaba::meta_space::MetaSpace::GetInstance()->GetRoomInstance(room_id);
room_ptr->EnterRoom(nick_name, [](){}, [](const alibaba::dps::DPSError& error_msg) {});
接收其他成员进入房间消息通知的API使用示例
iOS端(Objective-C):
//房间内其他人通过AIRBRoomChannelDelegate中的房间消息事件AIRBRoomChannelEventMessageReceived
//中具体的AIRBRoomChannelMessageTypeRoomMembersInOut消息来收到某成员进入房间的通知
- (void) onAIRBRoomChannelEvent:(AIRBRoomChannelEvent) event info:(NSDictionary*)info {
switch (event) {
case AIRBRoomChannelEventMessageReceived: {
AIRBRoomChannelMessageType type = [[info valueForKey:@"type"] intValue];
switch (type) {
case AIRBRoomChannelMessageTypeRoomMembersInOut:{
NSData *turnData = [[info valueForKey:@"data"] dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *dataDic = [NSJSONSerialization JSONObjectWithData:turnData options:NSJSONReadingMutableLeaves error:nil];
BOOL enter = [[dataDic valueForKey:@"enter"] boolValue];
if (enter) {
NSString* nick = [dataDic valueForKey:@"nick"];
NSLog(@"%@ 进入房间", nick);
}
... ...
}
}
Android端(Java):
// 监听房间事件
roomChannel.addEventHandler(new SampleRoomEventHandler() {
@Override
public void onEnterOrLeaveRoom(RoomInOutEvent event) {
String action = event.enter ? "进入" : "离开";
Logger.i(TAG, String.format("%s%s了房间", event.nick, action));
}
});
Web端(JavaScript):
// 获取事件列表
const { EventNameEnum } = window.RoomPaasSdk
// 监听房间进出事件
roomChannel.on(EventNameEnum.PaaSRoomEnter, (event) => {
const action = event.data.enter ? '进入' : '离开'
console.log(`${event.data.nick}${action}了房间`)
})
Windows端 (C++):
继承RoomEventListener,并重写OnRoomInOut函数
virtual void OnRoomInOut(const RoomInOutEventModel & event) override {
}
文档内容是否对您有帮助?