Polyv Help Center

Help Center

云课堂场景 聊天室

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

1 Feature Overview

The chatroom module includes features such as speaking, liking, 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 PLVLCChatroomViewModel

PLVLCChatroomViewModel is the core class of the chatroom in the Scene layer for the cloud classroom scenario. Since only one chatroom is allowed per application, PLVLCChatroomViewModel 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 PLVLCChatroomViewModel 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 via callbacks when the View layer needs to refresh the UI or update the list data.

2.1.1 Lifecycle

PLVLCChatroomViewModel 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:

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

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

2.1.2 Fetching Chat History

After initialization, the Common layer chatroom core class PLVChatroomPresenter automatically fetches the first page of chat history. The code for fetching more chat history is as follows:

[[PLVLCChatroomViewModel sharedViewModel] loadHistory];

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

2.1.3 Sending Messages

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

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

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

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

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

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

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

PLVLCChatroomViewModel allows setting multiple proxy listeners and provides the following interfaces for adding and removing listeners:

/// 增加PLVLCChatroomViewModelProtocol协议的监听者
/// @param delegate 待增加的监听者
/// @param delegateQueue 执行回调的队列
- (void)addDelegate:(id<PLVLCChatroomViewModelProtocol>)delegate delegateQueue:(dispatch_queue_t)delegateQueue;

/// 移除PLVLCChatroomViewModelProtocol协议的监听者
/// @param delegate 待移除的监听者
- (void)removeDelegate:(id<PLVLCChatroomViewModelProtocol>)delegate;

The protocol PLVLCChatroomViewModelProtocol mentioned in the above two interfaces provides the following proxy methods. These methods are used to notify the View layer via callbacks when the View layer needs to refresh the UI or update the list data:

@protocol PLVLCChatroomViewModelProtocol <NSObject>

@optional

#pragma mark 私聊

/// 本地发送了新的私聊消息
/// 用于刷新列表
- (void)chatroomManager_didSendQuestionMessage;

/// 通知socket接收到新的私聊(教师回答)消息,每次1条
/// 用于刷新列表、显示新消息提示
- (void)chatroomManager_didReceiveAnswerMessage;

#pragma mark 公聊

/// 返回本地发送的公聊消息(包含禁言的情况)
/// 用于刷新列表、发送弹幕
/// @param model 消息模型,不为空
- (void)chatroomManager_didSendMessage:(PLVChatModel *)model;

/// 返回socket接收到的公聊消息
/// 用于刷新列表、发送弹幕、显示新消息提示
/// @param modelArray 消息队列,不为空
- (void)chatroomManager_didReceiveMessages:(NSArray <PLVChatModel *> *)modelArray;

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

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

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

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

/// 上报管理员发布的消息文本,间隔8秒触发一次
/// @param content 管理员消息文本
- (void)chatroomManager_managerMessage:(NSString * )content;

/// 上报需插入弹幕的文本,间隔1秒触发一次
/// @param content 弹幕文本
- (void)chatroomManager_danmu:(NSString * )content;

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