本文主要介绍离开房间的API以及如何接收房间内其他成员离开房间的消息。
说明
某成员离开房间成功后,房间内的其他人都会收到通知。
离开房间
iOS端(Objective-C):
// 离开房间
[id<AIRBRoomChannelProtocol>room leaveRoom];
// 离开房间成功,需要通过实现AIRBRoomChannelProtocol的AIRBRoomChannelDelegate中的如下方法和事件来通知,
- (void) onAIRBRoomChannelEvent:(AIRBRoomChannelEvent) event info:(NSDictionary*)info{
switch (event) {
case AIRBRoomChannelEventLeft:
break;
.....
}
}
//离开房间失败,需要监听AIRBRoomChannelDelegate中的如下错误事件
- (void) onAIRBRoomChannelErrorWithCode:(AIRBErrorCode)code message:(NSString*)message{
switch (code) {
case AIRBRoomChannelFailedToLeaveRoom:
break;
.....
}
}
Android端(Java):
roomChannel.leaveRoom(new Callback<Void>() {
@Override
public void onSuccess(Void data) {
// 离开成功
}
@Override
public void onError(String errorMsg) {
// 离开失败
}
});
Web端(JavaScript):
roomChannel.leaveRoom()
Windows端(c++):
std::string room_id;
auto room_ptr = alibaba::meta_space::MetaSpace::GetInstance()->GetRoomInstance(room_id);
room_ptr->LeaveRoom([](){}, [](const alibaba::dps::DPSError& error_msg) {});
接收其他成员离开房间消息通知
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 {
}
文档内容是否对您有帮助?