实现礼物(基于NERoom)

更新时间: 2023/07/06 11:10:23

本文介绍语聊房 Demo 中如何实现发送礼物、接收礼物。

功能介绍

礼物在语聊房场景中是非常重要的功能,它可以让用户之间更加亲密,提高用户留存率。

礼物的实现流程如下图所示。

sequenceDiagram
    autonumber
    participant DemoClient as 客户端
    participant VoiceRoomKit as 语聊房业务代码(开源)
    participant DemoServer as Nemo服务端
    participant NERoomServer as NERoom 服务端
    participant NERoomSDK as NERoomSDK
    DemoClient->>VoiceRoomKit: sendBatchGift
    VoiceRoomKit->>DemoServer: 调用 Nemo 服务端的打赏API<br>nemo/entertainmentLive/live/batch/reward
    DemoServer->>DemoServer: 记录打赏数据,自行实现计费相关逻辑
    DemoServer->>NERoomServer: 调用NERoom服务端的发送聊天室自定消息接口  
    NERoomServer->>NERoomSDK: 发送消息 
    NERoomSDK-->>NERoomServer: 发送消息成功 
    NERoomServer-->>DemoServer: 消息发送成功
    DemoServer-->>VoiceRoomKit: 礼物发送成功
    VoiceRoomKit-->>DemoClient: 返回打赏成功
    NERoomSDK -->> VoiceRoomKit: onReceiveMessage
    VoiceRoomKit -->> DemoClient: 监听打赏消息(onReceiveBatchGift)
    DemoClient -->> DemoClient: 更新UI、特效等

语聊房 Demo 中的礼物功能,只是礼物赠送流程的展示,并不提供支付体系、结算体系、风控体系的相关指导,请自行实现相关业务逻辑。

NERoom 服务端的发送聊天室自定消息默认为高优先级消息,保障礼物消息被高优先级发送。

发送礼物

客户端调用 sendBatchGift 接口发送批量礼物。

该接口为语聊房业务代码(开源)封装的一个接口,具体可以参考语聊房 Demo 源码

示例代码

   NEVoiceRoomKit.getInstance().sendBatchGift(giftId, giftCount, userUuids, new NEVoiceRoomCallback<Unit>() {
        @Override
        public void onSuccess(@Nullable Unit unit) {
            
        }

        @Override
        public void onFailure(int code, @Nullable String msg) {

        }
    });

收到礼物消息

客户端监听 onReceiveBatchGift回调,接收礼物消息。

示例代码

    NEVoiceRoomKit.getInstance().addVoiceRoomListener(new NEVoiceRoomListener() {
        @Override
        public void onReceiveBatchGift(@NonNull NEVoiceRoomBatchGiftModel giftModel) {
                
        }
    });

此文档是否对你有帮助?
有帮助
去反馈
  • 功能介绍
  • 发送礼物
  • 收到礼物消息