本文介绍如何对房间内某些成员或全体成员发送自定义消息。
说明
此功能有两个接口:给房间内全体成员发送自定义消息、指定成员发送自定义消息。消息内容可以自定义,用于实现业务上的个性化功能。
全员自定义消息和指定成员自定义消息的接收方式相同。
API使用示例
给房间内全体成员发送自定义消息
iOS:
id<AIRBRoomChannelProtocol> room = [[AIRBRoomEngine sharedInstance] getRoomChannelWithRoomID:@"xxx"]
[room.chat sendCustomMessageToALL:@"你的自定义消息" onSuccess:^{
NSLog(@"发送成功");
} onFailure:^(NSString * _Nonnull errorMessage) {
NSLog(@"发送失败");
}];
Android:
chatService.sendCustomMessageToAll("body", new Callback<String>() {
@Override
public void onSuccess(String data) {
// 发送成功
}
@Override
public void onError(String errorMsg) {
// 发送失败
}
});
Web:
// 内容可以发送String或Object数据结构
chatService.sendCustomMessageToAll('内容')
// 发送Object
chatService.sendCustomMessageToAll({
count: 2,
avatar: 'https://img.png'
}).then(() => { console.log('发送成功') })
Windows:
auto room_ptr = alibaba::meta_space::MetaSpace::GetInstance()->GetRoomInstance(room_id);
std::shared_ptr<IChat> chat_plugin = std::dynamic_pointer_cast<IChat>(room_ptr->GetPlugin(PluginChat));
alibaba::chat::SendCustomMessageReq req;
chat_plugin->SendCustomMessage(req,[](const ::alibaba::chat::SendCustomMessageRsp& rsp) {
}, [](const alibaba::dps::DPSError& err) {
});
给房间内指定成员发送自定义消息
iOS:
//可以给指定的一组userID发送自定义消息,比如以下给userID为"111"和"222"的用户发送自定义消息
id<AIRBRoomChannelProtocol> room = [[AIRBRoomEngine sharedInstance] getRoomChannelWithRoomID:@"xxx"]
[self.room.chat sendCustomMessage:@"你的自定义消息" toUsers:@[@"111", @"2222"] onSuccess:^{
NSLog(@"发送成功");
} onFailure:^(NSString * _Nonnull errorMessage) {
NSLog(@"发送失败");
}];
Android:
List<String> userIds = new ArrayList<>();
userIds.add("111");
userIds.add("222");
chatService.sendCustomMessageToUsers("body", userIds, new Callback<String>() {
@Override
public void onSuccess(String data) {
// 发送成功
}
@Override
public void onError(String errorMsg) {
// 发送失败
}
});
Web:
// 与发送自定义消息给所有用户的接口相同,可以传String或Object
// 第二个参数为userId的数组
chatService.sendCustomMessageToUsers({
count: 2,
avatar: 'https://img.png'
}, ['userId1', userId2]).then(() => { console.log('发送成功') })
Windows:
auto room_ptr = alibaba::meta_space::MetaSpace::GetInstance()->GetRoomInstance(room_id);
std::shared_ptr<IChat> chat_plugin = std::dynamic_pointer_cast<IChat>(room_ptr->GetPlugin(PluginChat));
alibaba::chat::SendCustomMessageToUsersReq req;
chat_plugin->SendCustomMessageToUsers(req,[](const ::alibaba::chat::SendCustomMessageToUsersRsp & rsp) {
}, [](const alibaba::dps::DPSError& err) {
});
接收自定义消息通知的API使用示例
iOS:
// 需要设置AIRBRoomChannelProtocol的delegate并实现以下方法,监听如下事件,解析JSON内容
- (void) onAIRBRoomChannelEvent:(AIRBRoomChannelEvent) event info:(NSDictionary*)info
{
case AIRBRoomChannelEventMessageReceived: {
AIRBRoomChannelMessageType type = [[info valueForKey:@"type"] intValue];
switch (type)
{
case AIRBRoomChannelMessageTypeChatCustomMessageReceived:
{
NSLog(@"收到自定义消息:%@", [info valueForKey:@"data"]);
}
break;
... ...
}
Android:
chatService.addEventHandler(new SampleChatEventHandler() {
@Override
public void onCustomMessageReceived(CustomMessageEvent event) {
// 监听到自定义信息
}
});
Web:
// 获取事件列表
const { EventNameEnum } = window.RoomPaasSdk
// 监听接收自定义消息通知
chatService.on(EventNameEnum.PaaSChatCustomMessage, (event) => {
console.log(event.data)
})
Windows:
//继承ChatEventListener,并重写OnCustomMessageReceived方法
virtual void OnCustomMessageReceived(const std::string& event) override {
}
反馈
- 本页导读 (1)
文档反馈