Polyv Help Center

Help Center

带货场景 聊天室

Updated: 2023-04-17 15:27:57

1 Feature Overview

The chatroom module includes features such as sending messages, likes, chat history, and welcome messages. Compared to other modules, the chatroom module is more complex and extensive in terms of UI, interaction, and functionality. Therefore, designing, building, and maintaining a chatroom can be time-consuming and labor-intensive. We recommend directly using the chatroom module provided by Polyv, which is fully open-source and supports direct use as well as secondary development.

2 Core Class Introduction

2.1 PLVECChatroomViewModel

PLVECChatroomViewModel is the core class of the chatroom in the Scene layer for the e-commerce scenario. Since only one chatroom is allowed per application, PLVECChatroomViewModel adopts a singleton pattern. It is responsible for creating, holding, and destroying the instance object of the Common layer chatroom core class PLVChatroomPresenter, as well as communicating with the Common layer chatroom module. The core class PLVECChatroomViewModel provides the following functionalities:

  1. Provides an interface for the View layer to send messages;

  2. Manages the message models returned by the Common layer;

  3. Notifies the View layer through callbacks when the View layer needs to refresh the UI or update the list data.

2.1.1 Lifecycle

PLVECChatroomViewModel is a singleton class. When entering the live room and starting the chatroom, call the -setup method. When leaving the live room, call the -clear method. The code is as follows:

// 使用新的直播间数据启动聊天室管理器
[[PLVECChatroomViewModel sharedViewModel] setup];
// 退出前调用,用于资源释放、状态位清零
[[PLVECChatroomViewModel sharedViewModel] clear];

The "live room data" mentioned in the code comments refers to the roomData held by the PLVRoomDataManager singleton.

2.1.2 Retrieving Chat History

After the Common layer chatroom core class PLVChatroomPresenter is initialized, it automatically retrieves the first page of chat history. The code for retrieving more chat history is as follows:

[[PLVECChatroomViewModel sharedViewModel] loadHistory];

-loadHistory uses the presenter object initialized in the -setup method of 2.1.1 to call the Common layer chatroom module to retrieve chat history.

2.1.3 Sending Messages

The PLVECChatroomViewModel class provides the API for sending messages required in the e-commerce scenario. The specific interface definitions are as follows:

/// 发送文本消息
/// @param content 消息文本
/// @return YES表示数据将有更新,可等待收到回调后刷新列表;NO表示socket未登录或房间关闭,可进行toast提示
- (BOOL)sendSpeakMessage:(NSString *)content;

/// 发送礼物消息
/// @param data 礼物消息data字段
/// @param tip 礼物消息tip字段
/// @return 是否成功发送的布尔值
- (BOOL)sendGiftMessageWithData:(NSDictionary *)data tip:(NSString *)tip;

/// 发送自定义消息
/// @param event 自定义消息event字段
/// @param data 自定义消息data字段
/// @param tip 自定义消息tip字段
/// @param emitMode 自定义消息emitMode字段
/// @return 是否成功发送的布尔值
- (BOOL)sendCustomMessageWithEvent:(NSString *)event
                              data:(NSDictionary *)data
                               tip:(NSString * _Nullable)tip
                          emitMode:(int)emitMode;

/// 发送点赞消息
/// 点赞数的实时更新通过监听roomData的likeCount获得
- (void)sendLike;
2.1.4 Message Array

The PLVECChatroomViewModel class provides the following message list arrays as the data source for the chatroom View layer:

/// 全部消息数组
@property (nonatomic, strong, readonly) NSMutableArray <PLVChatModel *> *chatArray;
2.1.5 Listeners and Callbacks

PLVECChatroomViewModel provides the property delegate for setting callback listeners. The property delegate conforms to the protocol PLVECChatroomViewModelProtocol, which is used to notify the View layer through callbacks when the View layer needs to refresh the UI or update the list data:

@protocol PLVECChatroomViewModelProtocol <NSObject>

@optional

/// 返回本地发送的公聊消息(包含禁言的情况)
/// 用于刷新列表、滚动列表到底部
- (void)chatroomManager_didSendMessage;

/// 返回socket接收到的公聊消息
/// 用于刷新列表、显示新消息提示
- (void)chatroomManager_didReceiveMessages;

/// socket通知有消息被删除(1条或多条)
/// 用于刷新列表
- (void)chatroomManager_didMessageDeleted;

/// 获取历史聊天记录成功时触发
/// 用于刷新列表,停止【下拉加载更多】控件的动画
/// @param noMore 是否还有更多历史消息,YES表示已加载完,此时可隐藏【下拉加载更多】控件
/// @param first  是否是初次加载历史消息,初次加载需滚动列表到底部
- (void)chatroomManager_loadHistorySuccess:(BOOL)noMore firstTime:(BOOL)first;

/// 获取历史聊天消息失败时触发
/// 用于停止【下拉加载更多】控件的动画
- (void)chatroomManager_loadHistoryFailure;

/// 当前时间段内如果有用户登录聊天室(包括自己),间隔2秒触发一次
/// @param userArray 登录聊天室的用户数组,如果为nil,表示当前时间段内当前用户有登录事件
- (void)chatroomManager_loginUsers:(NSArray <PLVChatUser *> * _Nullable )userArray;

@end
联系客服,在线咨询
在线咨询