文档

发送弹幕消息

更新时间:

本文介绍如何发送弹幕消息。

说明

  • 接口第二个参数为extension,可以添加希望附带的信息,信息会透传到事件中和消息列表中。

API使用示例

iOS:

//有两个方法可以使用

//可以只发送弹幕内容
id<AIRBRoomChannelProtocol> room = [[AIRBRoomEngine sharedInstance] getRoomChannelWithRoomID:@"xxx"]
[room.chat sendComment:message onSuccess:^{
    NSLog(@"发送成功");
} onFailure:^(AIRBErrorCode code, NSString * _Nonnull message) {
    NSLog(@"发送失败"); 
}];

//或者,也可以携带自定义扩展字段
[room.chat sendComment:message extension:@{
      @"test" : @"test"
} onSuccess:^{
      NSLog(@"发送成功");
} onFailure:^(AIRBErrorCode code, NSString * _Nonnull message) {
      NSLog(@"发送失败");
}];

Android:

// 1. 普通发送
chatService.sendComment("弹幕信息", new Callback<String>() {
    @Override
    public void onSuccess(String data) {
        // 发送成功
    }

    @Override
    public void onError(String errorMsg) {
        // 发送失败
    }
});


// 2. 带拓展字段发送
HashMap<String, String> extension = new HashMap<>();
extension.put("key1", "value1");
extension.put("key2", "value2");
chatService.sendComment("弹幕信息", extension, new Callback<String>() {
    @Override
    public void onSuccess(String data) {
        // 发送成功
    }

    @Override
    public void onError(String errorMsg) {
        // 发送失败
    }
});

Web:

// 普通发送
chatService.sendComment('弹幕消息').then(() => { console.log('发送成功') })

// 带拓展字段发送
chatService.sendComment('弹幕消息', {
  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));
chat_plugin->SendComment("test", [](const std::string& comment_id) {
}, [](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 AIRBRoomChannelMessageTypeChatCommentReceived: 
           {
                NSData *turnData = [[info valueForKey:@"data"] dataUsingEncoding:NSUTF8StringEncoding];
                NSDictionary *dataDic = [NSJSONSerialization JSONObjectWithData:turnData options:NSJSONReadingMutableLeaves error:nil];
                NSLog(@"弹幕发送人userID:%@, 弹幕发送人昵称:%@, 弹幕内容:%@", [dataDic valueForKey:@"creatorOpenId"], [dataDic valueForKey:@"creatorNick"], [dataDic valueForKey:@"content"]);
           }
           break;
           ... ...
} 

Android:

chatService.addEventHandler(new SampleChatEventHandler() {
    @Override
    public void onCommentReceived(CommentEvent event) {
        // 监听到弹幕信息
    }
});

Web:

// 获取事件列表
const { EventNameEnum } = window.RoomPaasSdk
// 监听接收弹幕消息通知
chatService.on(EventNameEnum.PaaSChatReciveComment, (event) => {
  console.log(event.data)
})

Windows:

//继承ChatEventListener,并重写OnCommentReceived方法
virtual void OnCommentReceived(const CommentReceivedEventModel & event) override {

}

  • 本页导读 (1)
文档反馈