Polyv Help Center

Help Center

核心common 聊天室

Updated: 2023-04-27 09:43:40

1 Feature Overview

This module is located under the folder PolyvLiveCommonModule/Modules/Chatroom. It adopts the MVP pattern design to isolate the View layer from direct calls to the SDK layer's chatroom chatroomManager, and extracts and encapsulates common code for chatroomManager across multiple scenarios, making the View layer logic more concise.

2 Core Class Introduction

2.1 PLVChatroomPresenter

The PLVChatroomPresenter class is primarily responsible for socket login, receiving and sending socket messages. It also provides the following features:

  1. Real-time updates of live room data, including online user count, viewing popularity, and like count;
  2. Provides APIs for sending various types of messages and returns the messages encapsulated as data models;
  3. Loads historical chat records and returns the loading results and data encapsulated as data models to the Scene layer via callbacks;
  4. Listens for socket messages related to chat message reception and deletion, encapsulates new messages as data models, and notifies the Scene layer via callbacks.
2.1.1 Initialization

Initialize PLVChatroomPresenter in the Scene layer. The initialization method is defined as follows:

/// 初始化方法
/// @param count 每次调用接口获取的聊天消息条数,不得小于1
/// @param allow 是否允许使用分房间功能
- (instancetype)initWithLoadingHistoryCount:(NSUInteger)count childRoomAllow:(BOOL)allow;

Alternatively, you can use the -init method for initialization. In this case, the number of chat messages retrieved per API call defaults to 20, and the sub-room feature is disabled by default.

2.1.2 Setting Listeners

PLVChatroomPresenter provides the property delegate for setting callback listeners. Example code for initializing presenter and setting listeners is as follows (refer to the PLVLCChatroomViewModel class in the Demo for more details):

// 初始化聊天室Presenter并设置delegate
self.presenter = [[PLVChatroomPresenter alloc] initWithLoadingHistoryCount:10 childRoomAllow:YES];
self.presenter.delegate = self;

The property delegate conforms to the protocol PLVChatroomPresenterProtocol, which is used to notify the Scene layer of relevant socket messages from the chatroom module and chatroom-related data returned by HTTP interfaces (see 2.1.3 for details). The interface definitions of the protocol PLVChatroomPresenterProtocol are as follows:

/* PLVChatroomPresenter的协议 */
@protocol PLVChatroomPresenterProtocol <NSObject>

@optional

/// 获取历史聊天消息成功时触发
/// @param modelArray 聊天消息队列
/// @param noMore 是否还有更多历史消息,YES表示已加载完
- (void)chatroomPresenter_loadHistorySuccess:(NSArray <PLVChatModel *> *)modelArray noMore:(BOOL)noMore;

/// 获取历史聊天消息失败时触发
- (void)chatroomPresenter_loadHistoryFailure;

/// 返回socket接收到的消息
/// @param modelArray 消息队列,不为空
- (void)chatroomPresenter_didReceiveChatModels:(NSArray <PLVChatModel *> *)modelArray;

/// 返回socket接收到的教师回答消息
/// @param model 教师回答消息
- (void)chatroomPresenter_didReceiveAnswerChatModel:(PLVChatModel *)model;

/// socket通知已删除某条消息
/// @param msgId 被删除消息ID
- (void)chatroomPresenter_didMessageDeleted:(NSString *)msgId;

/// socket通知所有聊天消息被清空
- (void)chatroomPresenter_didAllMessageDeleted;

@end
2.1.3 Retrieving Chat History

PLVChatroomPresenter provides functionality for loading historical chat records. The method for loading historical chat records is defined as follows:

/// 加载历史聊天记录
- (void)loadHistory;

The number of chat messages loaded each time corresponds to the value of the parameter count passed during initialization, or defaults to 20 (when using the -init method for initialization). After each successful HTTP interface call, the currently loaded page number is recorded. The next call to the method -loadHistory will automatically load the next page of data.

Whether loading chat records succeeds or fails, the Scene layer will be notified via the delegate method mentioned in 2.1.2.

Note: When initializing an instance of PLVChatroomPresenter, the first page of chat records is automatically retrieved, triggering the relevant callbacks for loading historical chat records.

2.1.4 Sending Messages

The PLVChatroomPresenter class provides APIs for sending various types of messages and returns the messages encapsulated as data models. The specific interface definitions are as follows:

/// 发送私聊提问消息
/// @param content 消息文本
/// @return 消息数据模型
- (PLVChatModel * _Nullable)sendQuesstionMessage:(NSString *)content;

/// 发送文本消息
/// @param content 消息文本
/// @return 消息数据模型
- (PLVChatModel * _Nullable)sendSpeakMessage:(NSString *)content;

/// 发送图片消息
/// @param image 图片
/// @return 消息数据模型
- (PLVChatModel * _Nullable)sendImageMessage:(UIImage *)image;

/// 发送自定义消息
/// @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;

/// 发送点赞消息
- (void)sendLike;

When a message is sent successfully, the interface returns the message data model; on failure, it returns nil.

2.1.5 Muting and Kicking Users

On the instructor side, muting and kicking operations are also supported. When initializing an instance of PLVChatroomPresenter, you can set the property specialRole to YES, indicating that the currently logged-in user has a special identity (instructor). In this case, the logged-in user will not be affected by whether the chatroom is closed or muted, and can send muting or kicking messages to other online members.

self.presenter.specialRole = YES;

The Scene layer can directly call the following methods of the SDK's PLVChatroomManager to send mute/unmute and kick messages:

/// 发送禁言消息,讲师端专用接口
/// @param banned YES-禁言 NO-取消禁言
/// @param userId 被禁言/取消禁言的用户ID
- (BOOL)sendBandMessage:(BOOL)banned bannedUserId:(NSString *)userId;

/// 发送踢人消息,讲师端专用接口
/// @param userId 被踢出的用户ID
- (BOOL)sendKickMessageWithUserId:(NSString *)userId;
2.1.5 Destruction

When the Scene layer exits the current scene, it performs destruction operations on the presenter instance. The destruction method is defined as follows:

/// 销毁方法
/// 退出前调用,用于资源释放、状态位清零
- (void)destroy;
联系客服,在线咨询
在线咨询