实现聊天消息

更新时间: 2022/11/04 10:02:17

为了提升主唱和观众之间的互动性,您可以增加文字聊天和礼物发送的功能。本文介绍如何通过 IM 的聊天室实现消息收发。

详细的说明请参见聊天室消息收发

1. 登录 IM

调用 login 接口登录 IM,详细的登录方法请参见 登录管理

手动登录 IM 的示例代码如下:

[[[NIMSDK sharedSDK] loginManager] login:loginAccount
                                   token:loginToken
                                authType:authType
                                loginExt:loginExt
                              completion:^(NSError *error) {
    if (error == nil)
    {
        NSLog(@"NIM: login onSuccess");
    }
    else
    {
        NSLog(@"NIM: login onFailed, code is %zd", error.code);
    }
}];

2. 进入聊天室

用户要在聊天室收发消息,必须调用 enterChatRoom 接口先进入聊天室。聊天室只允许用户手动进入,无法进行邀请。

通过非独立模式(先登录 IM,再进入聊天室)进入聊天室的示例代码如下:

NIMChatroomEnterRequest *request = [[NIMChatroomEnterRequest alloc] init];
request.roomId = roomId;
[[NIMSDK sharedSDK].chatroomManager enterChatroom:request completion:^(NSError *error, NIMChatroom *chatroom, NIMChatroomMember *me) {
    if (!error)
    {
        NSLog(@"NIM: enterChatroom onSuccess");
    } else {
        NSLog(@"NIM: enterChatroom onFailed, code is %zd", error.code);
    }
}];

3. 发送和接收文字聊天消息

主唱和观众之间通过聊天室消息实现文字聊天功能。

实现流程

uml diagram
  1. 聊天室成员调用 NIMChatManagerDelegateonRecvMessages: 方法监听消息接收。

  2. 发送者构建文本消息并调用 sendMessage:toSession:error:方法发送消息。

    说明:需要将sendMessage:toSession:error:中的 type 参数设置为 NIMSessionTypeChatroom

  3. 触发 onRecvMessages: 回调,聊天室成员收到文本消息。

示例代码

以下示例代码展示如何实现发送文字聊天消息。

NIMMessage *message = [[NIMMessage alloc] init];
message.text = text;
NIMSession *session = [NIMSession session:roomId type:NIMSessionTypeChatroom];
[[NIMSDK sharedSDK].chatManager sendMessage:message toSession:session completion:^(NSError *err) {
    if (!error)
    {
        NSLog(@"NIM: sendMessage onSuccess");
    } else {
        NSLog(@"NIM: sendMessage onFailed, code is %zd", error.code);
    } 
}];

以下示例代码展示如何实现接收文字聊天消息。 页面声明:

@interface NIMSessionViewController : UIViewController<NIMChatManagerDelegate>
@end

实现:

@implementation NTESChatroomViewController
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    // 添加自己为ChatManager的回调。(NTESChatroomViewController实现了NIMChatManagerDelegate)
    [[NIMSDK sharedSDK].chatManager addDelegate:self];
}

- (void)dealloc
{
    [[NIMSDK sharedSDK].chatManager removeDelegate:self];
}

- (void)onRecvMessages:(NSArray<NIMMessage *> *)messages
{
    NSLog(@"NIM: received messages");
}
@end

4. 发送和接收礼物

观众在房间内可以发送礼物给主唱或合唱者,可通过发送自定义的消息来实现。发送端需要在自定义消息中携带礼物 ID,其他端在接收到礼物消息后解析出礼物 ID,并播放对应动效。

  1. 发送礼物。

    发送端通过业务服务器,调用 IM 服务端的 API(chatroom/sendMsg.action)发送礼物,调用时将业务参数 highPriority 设置为 true,从而将该自定义消息设置为高优先级消息。详情请参见发送聊天室消息

    为了避免礼物消息数量过多超过高优消息每秒上限,建议自行开发如下两个方案,并将两者组合使用:

    方案
    说明
    方案一 根据礼物的级别分配不同的策略。例如:普通礼物每秒 5 条,高级礼物每秒 5 条,同时高级礼物没用完的条数配额可以分给普通礼物。
    方案二 如普通礼物数量较多,则将多个普通礼物合并到一条消息中发送。如果数量太多,合并的普通礼物消息数量也超过了每秒上限,则丢弃部分普通礼物消息的发送。因为在礼物数量很大的情况下,用户不会在意普通礼物展示是否少量丢失。
  2. 接收礼物。

    监听 onRecvMessages回调,在收到礼物时,就可以接收到通知。

    页面声明:

    @interface NIMSessionViewController : UIViewController<NIMChatManagerDelegate>
    @end
    

    实现接收礼物的示例代码如下:

    @implementation NTESChatroomViewController
    - (void)viewWillAppear:(BOOL)animated
    {
        [super viewWillAppear:animated];
        // 添加自己为ChatManager的回调。(NTESChatroomViewController实现了NIMChatManagerDelegate)
        [[NIMSDK sharedSDK].chatManager addDelegate:self];
    }
    
    - (void)dealloc
    {
        [[NIMSDK sharedSDK].chatManager removeDelegate:self];
    }
    
    - (void)onRecvMessages:(NSArray<NIMMessage *> *)messages
    {
        NSLog(@"NIM: received messages");
    }
    @end  
    

5. 离开聊天室

用户调用exitChatRoom 接口离开聊天室。离开聊天室后,会断开聊天室对应的链接,不再收到该聊天室的任何消息。

该接口没有回调。

离开聊天室的示例代码如下:

[[NIMSDK sharedSDK].chatroomManager exitChatroom:roomId completion:^(NSError * __nullable error) 
{
    if (!error)
    {
        NSLog(@"NIM: sendMessage onSuccess");
    } else 
    {
        NSLog(@"NIM: sendMessage onFailed, code is %zd", error.code);
    } 
}];
此文档是否对你有帮助?
有帮助
去反馈
  • 1. 登录 IM
  • 2. 进入聊天室
  • 3. 发送和接收文字聊天消息
  • 实现流程
  • 示例代码
  • 4. 发送和接收礼物
  • 5. 离开聊天室