5_5-手机开播场景-聊天室
1 Feature Overview
The chatroom module includes features such as speaking, viewing chat history, replying to messages, kicking users, and muting users. 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 PLVLSChatroomViewModel
PLVLSChatroomViewModel is the core class of the Scene layer chatroom for the mobile live streaming scenario. Since only one chatroom is allowed per application, PLVLSChatroomViewModel 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 PLVLSChatroomViewModel provides the following functionalities:
Provides an interface for the View layer to send messages;
Manages the message models returned by the Common layer;
Notifies the View layer through callbacks when the View layer needs to refresh the UI or update list data.
2.1.1 Lifecycle
PLVLSChatroomViewModel is a singleton class. When entering a live room and starting the chatroom, call the -setup method. When leaving the live room, call the -clear method. The code is as follows:
// 使用新的直播间数据启动聊天室管理器
[[PLVLSChatroomViewModel sharedViewModel] setup];
// 退出前调用,用于资源释放、状态位清零
[[PLVLSChatroomViewModel 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 default number of messages per page is 20. The code for retrieving more chat history is as follows:
[[PLVLSChatroomViewModel sharedViewModel] loadHistory];
2.1.3 Sending Messages and Replying to Messages
The PLVLSChatroomViewModel class provides the API for sending messages required in the mobile live streaming scenario. The specific interface definitions are as follows:
/// 发送文本消息
/// @param content 消息文本
/// @param replyChatModel 回复消息模型(非回复消息该字段为nil)
/// @return YES表示数据将有更新,可等待收到回调后刷新列表;NO表示socket未登录或房间关闭,可进行toast提示
- (BOOL)sendSpeakMessage:(NSString *)content replyChatModel:(PLVChatModel * _Nullable)replyChatModel;
/// 发送图片消息
/// @param image 图片
/// @return YES表示数据将有更新,可等待收到回调后刷新列表;NO表示socket未登录或房间关闭,可进行toast提示
- (BOOL)sendImageMessage:(UIImage *)image;
2.1.4 Message Array
The PLVLSChatroomViewModel 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
PLVLSChatroomViewModel provides the property delegate for setting callback listeners. The property delegate conforms to the protocol PLVLSChatroomViewModelProtocol, which is used to notify the View layer through callbacks when the View layer needs to refresh the UI or update list data:
@protocol PLVLSChatroomViewModelProtocol <NSObject>
@optional
/// 返回本地发送的公聊消息(包含禁言的情况)
/// 用于刷新列表、滚动列表到底部
- (void)chatroomViewModel_didSendMessage;
/// 返回socket接收到的公聊消息
/// 用于刷新列表、显示新消息提示
- (void)chatroomViewModel_didReceiveMessages;
/// socket通知有消息被删除(1条或多条)
/// 用于刷新列表
- (void)chatroomViewModel_didMessageDeleted;
/// 获取历史聊天记录成功时触发
/// 用于刷新列表,停止【下拉加载更多】控件的动画
/// @param noMore 是否还有更多历史消息,YES表示已加载完,此时可隐藏【下拉加载更多】控件
/// @param first 是否是初次加载历史消息,初次加载需滚动列表到底部
- (void)chatroomViewModel_loadHistorySuccess:(BOOL)noMore firstTime:(BOOL)first;
/// 获取历史聊天消息失败时触发
/// 用于停止【下拉加载更多】控件的动画
- (void)chatroomViewModel_loadHistoryFailure;
@end
