聊天室管理


环信聊天室模型支持最大成员数为5000,和群组不同,聊天室内成员离线后,服务器当监听到此成员不在线后不再会给此成员再发推送。

  • 支持最大成员5000;
  • 环信的聊天室内有所有者,管理员和游客三种身份;
  • 支持禁言,黑名单,踢人等操作;
  • 不支持客户端邀请;
  • 不支持 REST 邀请。
  • 聊天室API通常是同步操作,需要在单独的线程中执行,如需使用异步API,请使用async前缀对应的API

服务器端

客户端

注册聊天室模块回调:

//EMChatroomManagerDelegate
//注册聊天室回调
[[EMClient sharedClient].roomManager addDelegate:self delegateQueue:nil];

//移除聊天室回调
[[EMClient sharedClient].roomManager removeDelegate:self];

分页获取聊天室列表

/*!
 *  从服务器获取指定数目的聊天室
 *
 *  同步方法,会阻塞当前线程
 *
 *  @param aPageNum         获取第几页
 *  @param aPageSize        获取多少条
 *  @param pError           出错信息
 *
 *  @return 聊天室列表<EMChatroom>
 */
- (EMPageResult *)getChatroomsFromServerWithPage:(NSInteger)aPageNum
                                        pageSize:(NSInteger)aPageSize
                                           error:(EMError **)pError;

/*!
 *  从服务器获取指定数目的聊天室
 *
 *  @param aPageNum              获取第几页
 *  @param aPageSize             获取多少条
 *  @param aCompletionBlock      完成的回调
 */

- (void)getChatroomsFromServerWithPage:(NSInteger)aPageNum
                              pageSize:(NSInteger)aPageSize
                            completion:(void (^)(EMPageResult *aResult, EMError *aError))aCompletionBlock;

//调用:
//EMError *error = nil;
//NSArray *list = [[EMClient sharedClient].roomManager getChatroomsFromServerWithPage:0 pageSize:50 error:&error];

加入聊天室

/*!
 *  加入聊天室
 *
 *  同步方法,会阻塞当前线程
 *
 *  @param aChatroomId  聊天室的ID
 *  @param pError       错误信息
 *
 *  @result 所加入的聊天室
 */
- (EMChatroom *)joinChatroom:(NSString *)aChatroomId
                       error:(EMError **)pError;
                       
/*!
 *  加入聊天室
 *
 *  @param aChatroomId           聊天室的ID
 *  @param aCompletionBlock      完成的回调
 */
- (void)joinChatroom:(NSString *)aChatroomId
          completion:(void (^)(EMChatroom *aChatroom, EMError *aError))aCompletionBlock; 

//调用:
//EMError *error = nil;
//EMChatroom *chatroom = [[EMClient sharedClient].roomManager joinChatroom:@"chatroomId" error:&error];                           

离开聊天室

/*!
 *  退出聊天室
 *
 *  同步方法,会阻塞当前线程
 *
 *  @param aChatroomId  聊天室ID
 *  @param pError       错误信息
 *
 *  @result 退出的聊天室,失败返回nil
 */
- (EMChatroom *)leaveChatroom:(NSString *)aChatroomId
                        error:(EMError **)pError;
                        
/*!
 *  退出聊天室
 *
 *  @param aChatroomId          聊天室ID
 *  @param aCompletionBlock      完成的回调
 */
- (void)leaveChatroom:(NSString *)aChatroomId
           completion:(void (^)(EMError *aError))aCompletionBlock;   

//调用:
//EMError *error = nil;
//[[EMClient sharedClient].roomManager leaveChatroom:@"chatroomId" error:&error];

获取聊天室详情

/*!
 *  获取聊天室详情
 *  
 *  同步方法,会阻塞当前线程
 *
 *  @param aChatroomId           聊天室ID
 *  @param pError                错误信息
 *
 *  @return    聊天室
 */
- (EMChatroom *)getChatroomSpecificationFromServerWithId:(NSString *)aChatroomId
                                                   error:(EMError **)pError;

/*!
 *  获取聊天室详情
 *
 *  @param aChatroomId           聊天室ID
 *  @param aCompletionBlock      完成的回调
 *
 */
- (void)getChatroomSpecificationFromServerWithId:(NSString *)aChatroomId
                                      completion:(void (^)(EMChatroom *aChatroom, EMError *aError))aCompletionBlock;
                               
//调用                                 
EMError *error = nil;
EMChatroom *room = [[EMClient sharedClient].roomManager getChatroomSpecificationFromServerWithId:@"roomId"error:&error];

分页获取聊天室成员列表

/*!
 *  获取聊天室成员列表
 *
 *  @param aChatroomId      聊天室ID
 *  @param aCursor          游标
 *  @param aPageSize        获取多少条
 *  @param pError           错误信息
 *
 *  @return    列表和游标
 */
- (EMCursorResult *)getChatroomMemberListFromServerWithId:(NSString *)aChatroomId
                                                   cursor:(NSString *)aCursor
                                                 pageSize:(NSInteger)aPageSize
                                                    error:(EMError **)pError;

/*!
 *  获取聊天室成员列表
 *
 *  @param aChatroomId      聊天室ID
 *  @param aCursor          游标
 *  @param aPageSize        获取多少条
 *  @param aCompletionBlock 完成的回调
 */
- (void)getChatroomMemberListFromServerWithId:(NSString *)aChatroomId
                                       cursor:(NSString *)aCursor
                                     pageSize:(NSInteger)aPageSize
                                   completion:(void (^)(EMCursorResult *aResult, EMError *aError))aCompletionBlock;
                               
//调用                                 
EMError *error = nil;
EMCursorResult *result = [[EMClient sharedClient].roomManager getChatroomMemberListFromServerWithId:@"roomId" cursor:@"cursor" pageSize:50 error:&error];

分页获取聊天室黑名单列表

需要Owner或Admin权限

/*!
 *  获取聊天室黑名单列表, 需要owner/admin权限
 *
 *  @param aChatroomId      聊天室ID
 *  @param aPageNum         获取第几页
 *  @param aPageSize        获取多少条
 *  @param pError           错误信息
 */
- (NSArray *)getChatroomBlacklistFromServerWithId:(NSString *)aChatroomId
                                       pageNumber:(NSInteger)aPageNum
                                         pageSize:(NSInteger)aPageSize
                                            error:(EMError **)pError;

/*!
 *  获取聊天室黑名单列表, 需要owner/admin权限
 *
 *  @param aChatroomId      聊天室ID
 *  @param aPageNum         获取第几页
 *  @param aPageSize        获取多少条
 *  @param aCompletionBlock 完成的回调
 */
- (void)getChatroomBlacklistFromServerWithId:(NSString *)aChatroomId
                                  pageNumber:(NSInteger)aPageNum
                                    pageSize:(NSInteger)aPageSize
                                  completion:(void (^)(NSArray *aList, EMError *aError))aCompletionBlock;
                               
//调用                                 
EMError *error = nil;
NSArray *list = [[EMClient sharedClient].roomManager getChatroomBlacklistFromServerWithId:@"roomId" pageNumber:1 pageSize:50 error:&error];

分页获取聊天室禁言列表

/*!
 *  获取聊天室被禁言列表
 *
 *  @param aChatroomId      聊天室ID
 *  @param aPageNum         获取第几页
 *  @param aPageSize        获取多少条
 *  @param pError           错误信息
 */
- (NSArray *)getChatroomMuteListFromServerWithId:(NSString *)aChatroomId
                                      pageNumber:(NSInteger)aPageNum
                                        pageSize:(NSInteger)aPageSize
                                           error:(EMError **)pError;

/*!
 *  获取聊天室被禁言列表
 *
 *  @param aChatroomId      聊天室ID
 *  @param aPageNum         获取第几页
 *  @param aPageSize        获取多少条
 *  @param aCompletionBlock 完成的回调
 */
- (void)getChatroomMuteListFromServerWithId:(NSString *)aChatroomId
                                 pageNumber:(NSInteger)aPageNum
                                   pageSize:(NSInteger)aPageSize
                                 completion:(void (^)(NSArray *aList, EMError *aError))aCompletionBlock;
                               
//调用                                 
EMError *error = nil;
NSArray *list = [[EMClient sharedClient].roomManager getChatroomMuteListFromServerWithId:@"roomId" pageNumber:1 pageSize:50 error:&error];

更新聊天室名称

需要Owner权限

/*!
 *  更改聊天室主题, 需要owner权限
 *
 *  同步方法,会阻塞当前线程
 *
 *  @param aSubject     新主题
 *  @param aChatroomId  聊天室ID
 *  @param pError       错误信息
 *
 *  @result    聊天室对象
 */
- (EMChatroom *)updateSubject:(NSString *)aSubject
                  forChatroom:(NSString *)aChatroomId
                        error:(EMError **)pError;

/*!
 *  更改聊天室主题, 需要owner权限
 *
 *  @param aSubject         新主题
 *  @param aChatroomId      聊天室ID
 *  @param aCompletionBlock 完成的回调
 */
- (void)updateSubject:(NSString *)aSubject
          forChatroom:(NSString *)aChatroomId
           completion:(void (^)(EMChatroom *aChatroom, EMError *aError))aCompletionBlock;
                               
//调用                                 
EMError *error = nil;
EMChatroom *room = [[EMClient sharedClient].roomManager updateSubject:@"newSubject" forChatroom:@"roomId" error:&error];

更新聊天室说明

需要Owner权限

/*!
 *  更改聊天室说明信息, 需要owner权限
 *
 *  同步方法,会阻塞当前线程
 *
 *  @param aDescription 说明信息
 *  @param aChatroomId  聊天室ID
 *  @param pError       错误信息
 *
 *  @result    聊天室对象
 */
- (EMChatroom *)updateDescription:(NSString *)aDescription
                      forChatroom:(NSString *)aChatroomId
                            error:(EMError **)pError;

/*!
 *  更改聊天室说明信息, 需要owner权限
 *
 *  @param aDescription     说明信息
 *  @param aChatroomId      聊天室ID
 *  @param aCompletionBlock 完成的回调
 */
- (void)updateDescription:(NSString *)aDescription
              forChatroom:(NSString *)aChatroomId
               completion:(void (^)(EMChatroom *aChatroom, EMError *aError))aCompletionBlock;
                               
//调用                                 
EMError *error = nil;
EMChatroom *room = [[EMClient sharedClient].roomManager updateDescription:@"newDes" forChatroom:@"roomId" error:&error];

获取聊天室公告

/*!
 *  获取聊天室公告
 *
 *  @param aChatroomId      聊天室ID
 *  @param pError           错误信息
 *
 *  @return    聊天室公告
 */
- (NSString *)getChatroomAnnouncementWithId:(NSString *)aChatroomId
                                      error:(EMError **)pError;

/*!
 *  获取聊天室公告
 *
 *  @param aChatroomId      聊天室ID
 *  @param aCompletionBlock 完成的回调
 */
- (void)getChatroomAnnouncementWithId:(NSString *)aChatroomId
                           completion:(void (^)(NSString *aAnnouncement, EMError *aError))aCompletionBlock;

//调用
[[EMClient sharedClient].roomManager getChatroomAnnouncementWithId:@"chatroomId"
                                                        completion:^(NSString *aAnnouncement, EMError *aError) {
                                                            if (!aError) {
                                                                NSLog(@"获取成功");
                                                            }
                                                        }];

更新聊天室公告

/*!
 *  修改聊天室公告,需要Owner / Admin权限
 *
 *  @param aChatroomId      聊天室ID
 *  @param aAnnouncement    群公告
 *  @param pError           错误信息
 *
 *  @result    聊天室实例
 */
- (EMChatroom *)updateChatroomAnnouncementWithId:(NSString *)aChatroomId
                                    announcement:(NSString *)aAnnouncement
                                           error:(EMError **)pError;

/*!
 *  修改聊天室公告,需要Owner / Admin权限
 *
 *  @param aChatroomId      聊天室ID
 *  @param aAnnouncement    群公告
 *  @param aCompletionBlock 完成的回调
 */
- (void)updateChatroomAnnouncementWithId:(NSString *)aChatroomId
                            announcement:(NSString *)aAnnouncement
                              completion:(void (^)(EMChatroom *aChatroom, EMError *aError))aCompletionBlock;

//调用
[[EMClient sharedClient].roomManager updateChatroomAnnouncementWithId:@"chatroomId"
                                                         announcement:@"announcement"
                                                           completion:^(EMChatroom *aChatroom, EMError *aError) {
                                                             if (!aError) {
                                                                 NSLog(@"修改成功");
                                                             }
                                                         }];

聊天室owner/Admin修改聊天室公告时,其它成员会收到聊天室公告有更新的通知

/*!
 *  聊天室公告有更新
 *
 *  @param aChatroom        聊天室
 *  @param aAnnouncement    公告
 */
- (void)chatroomAnnouncementDidUpdate:(EMChatroom *)aChatroom
                         announcement:(NSString *)aAnnouncement;

将成员移出聊天室

需要Owner或Admin权限

/*!
 *  将成员移出聊天室, 需要owner/admin权限
 *
 *  同步方法,会阻塞当前线程
 *
 *  @param aMembers     要移出的用户列表
 *  @param aChatroomId  聊天室ID
 *  @param pError       错误信息
 *
 *  @result    聊天室实例
 */
- (EMChatroom *)removeMembers:(NSArray *)aMembers
                 fromChatroom:(NSString *)aChatroomId
                        error:(EMError **)pError;

/*!
 *  将成员移出聊天室, 需要owner/admin权限
 *
 *  @param aMembers         要移出的用户列表
 *  @param aChatroomId      聊天室ID
 *  @param aCompletionBlock 完成的回调
 */
- (void)removeMembers:(NSArray *)aMembers
         fromChatroom:(NSString *)aChatroomId
           completion:(void (^)(EMChatroom *aChatroom, EMError *aError))aCompletionBlock;
                               
//调用                                 
EMError *error = nil;
EMChatroom *room = [[EMClient sharedClient].roomManager removeMembers:@[@"username"] fromChatroom:@"roomId" error:&error];

将用户加到聊天室黑名单

需要Owner或Admin权限

/*!
 *  加人到聊天室黑名单
 *
 *  同步方法,会阻塞当前线程
 *
 *  @param aMembers     要加入黑名单的用户
 *  @param aChatroomId  聊天室ID
 *  @param pError       错误信息
 *
 *  @result    聊天室实例
 */
- (EMChatroom *)blockMembers:(NSArray *)aMembers
                fromChatroom:(NSString *)aChatroomId
                       error:(EMError **)pError;

/*!
 *  加人到聊天室黑名单
 *
 *  @param aMembers         要加入黑名单的用户
 *  @param aChatroomId      聊天室ID
 *  @param aCompletionBlock 完成的回调
 */
- (void)blockMembers:(NSArray *)aMembers
        fromChatroom:(NSString *)aChatroomId
          completion:(void (^)(EMChatroom *aChatroom, EMError *aError))aCompletionBlock;
                               
//调用                                 
EMError *error = nil;
EMChatroom *room = [[EMClient sharedClient].roomManager blockMembers:@[@"username"] fromChatroom:@"roomId" error:&error];

将用户移出聊天室黑名单

需要Owner或Admin权限

/*!
 *  从聊天室黑名单中减人
 *
 *  同步方法,会阻塞当前线程
 *
 *  @param aMembers     要从黑名单中移除的用户名列表
 *  @param aChatroomId  聊天室ID
 *  @param pError       错误信息
 *
 *  @result    聊天室对象
 */
- (EMChatroom *)unblockMembers:(NSArray *)aMembers
                  fromChatroom:(NSString *)aChatroomId
                         error:(EMError **)pError;

/*!
 *  从聊天室黑名单中减人
 *
 *  @param aMembers         要从黑名单中移除的用户名列表
 *  @param aChatroomId      聊天室ID
 *  @param aCompletionBlock 完成的回调
 */
- (void)unblockMembers:(NSArray *)aMembers
          fromChatroom:(NSString *)aChatroomId
            completion:(void (^)(EMChatroom *aChatroom, EMError *aError))aCompletionBlock;
                               
//调用                                 
EMError *error = nil;
EMChatroom *room = [[EMClient sharedClient].roomManager unblockMembers:@[@"username"] fromChatroom:@"roomId" error:&error];

改变聊天室创建者

需要Owner权限

/*!
 *  改变聊天室创建者,需要Owner权限
 *
 *  同步方法,会阻塞当前线程
 *
 *  @param aChatroomId  聊天室ID
 *  @param aNewOwner    新Owner
 *  @param pError       错误信息
 *
 *  @result    聊天室实例
 */
- (EMChatroom *)updateChatroomOwner:(NSString *)aChatroomId
                           newOwner:(NSString *)aNewOwner
                              error:(EMError **)pError;

/*!
 *  改变聊天室创建者,需要Owner权限
 *
 *  @param aChatroomId      聊天室ID
 *  @param aNewOwner        新Owner
 *  @param aCompletionBlock 完成的回调
 */
- (void)updateChatroomOwner:(NSString *)aChatroomId
                   newOwner:(NSString *)aNewOwner
                 completion:(void (^)(EMChatroom *aChatroom, EMError *aError))aCompletionBlock;
                               
//调用                                 
EMError *error = nil;
EMChatroom *room = [[EMClient sharedClient].roomManager updateChatroomOwner:@"roomId" newOwner:@"newOwner" error:&error];

添加聊天室管理员

需要Owner权限

/*!
 *  添加聊天室管理员,需要Owner权限
 *
 *  同步方法,会阻塞当前线程
 *
 *  @param aAdmin       要添加的管理员
 *  @param aChatroomId  聊天室ID
 *  @param pError       错误信息
 *
 *  @result    聊天室实例
 */
- (EMChatroom *)addAdmin:(NSString *)aAdmin
              toChatroom:(NSString *)aChatroomId
                   error:(EMError **)pError;

/*!
 *  添加聊天室管理员,需要Owner权限
 *
 *  @param aAdmin           要添加的群组管理员
 *  @param aChatroomId      聊天室ID
 *  @param aCompletionBlock 完成的回调
 */
- (void)addAdmin:(NSString *)aAdmin
      toChatroom:(NSString *)aChatroomId
      completion:(void (^)(EMChatroom *aChatroomp, EMError *aError))aCompletionBlock;
                               
//调用                                 
EMError *error = nil;
EMChatroom *room = [[EMClient sharedClient].roomManager addAdmin:@"adminName" toChatroom:@"roomId" error:&error];

移除聊天室管理员

需要Owner权限

/*!
 *  移除聊天室管理员,需要Owner权限
 *
 *  同步方法,会阻塞当前线程
 *
 *  @param aAdmin       要移除的群组管理员
 *  @param aChatroomId  聊天室ID
 *  @param pError       错误信息
 *
 *  @result    聊天室实例
 */
- (EMChatroom *)removeAdmin:(NSString *)aAdmin
               fromChatroom:(NSString *)aChatroomId
                      error:(EMError **)pError;

/*!
 *  移除聊天室管理员,需要Owner权限
 *
 *  @param aAdmin           要添加的群组管理员
 *  @param aChatroomId      聊天室ID
 *  @param aCompletionBlock 完成的回调
 */
- (void)removeAdmin:(NSString *)aAdmin
       fromChatroom:(NSString *)aChatroomId
         completion:(void (^)(EMChatroom *aChatroom, EMError *aError))aCompletionBlock;
                               
//调用                                 
EMError *error = nil;
EMChatroom *room = [[EMClient sharedClient].roomManager removeAdmin:@"adminName" fromChatroom:@"roomId" error:&error];

禁言聊天室成员

权限高者可禁言权限低者,反之不允许

/*!
 *  将一组成员禁言,需要Owner / Admin权限
 *
 *  同步方法,会阻塞当前线程
 *
 *  @param aMuteMembers         要禁言的成员列表<NSString>
 *  @param aMuteMilliseconds    禁言时长
 *  @param aChatroomId          聊天室ID
 *  @param pError               错误信息
 *
 *  @result    聊天室实例
 */
- (EMChatroom *)muteMembers:(NSArray *)aMuteMembers
           muteMilliseconds:(NSInteger)aMuteMilliseconds
               fromChatroom:(NSString *)aChatroomId
                      error:(EMError **)pError;

/*!
 *  将一组成员禁言,需要Owner / Admin权限
 *
 *  @param aMuteMembers         要禁言的成员列表<NSString>
 *  @param aMuteMilliseconds    禁言时长
 *  @param aChatroomId          聊天室ID
 *  @param aCompletionBlock     完成的回调
 */
- (void)muteMembers:(NSArray *)aMuteMembers
   muteMilliseconds:(NSInteger)aMuteMilliseconds
       fromChatroom:(NSString *)aChatroomId
         completion:(void (^)(EMChatroom *aChatroom, EMError *aError))aCompletionBlock;
                               
//调用                                 
EMError *error = nil;
EMChatroom *room = [[EMClient sharedClient].roomManager muteMembers:@[@"userName"] muteMilliseconds:100 aChatroomId:@"roomId" error:&error];

解除禁言

权限高者可禁言权限低者,反之不允许

/*!
 *  解除禁言,需要Owner / Admin权限
 *
 *  同步方法,会阻塞当前线程
 *
 *  @param aMuteMembers     被解除的列表<NSString>
 *  @param aChatroomId      聊天室ID
 *  @param pError           错误信息
 *
 *  @result    聊天室实例
 */
- (EMChatroom *)unmuteMembers:(NSArray *)aMembers
                 fromChatroom:(NSString *)aChatroomId
                        error:(EMError **)pError;

/*!
 *  解除禁言,需要Owner / Admin权限
 *
 *  @param aMuteMembers     被解除的列表<NSString>
 *  @param aChatroomId      聊天室ID
 *  @param aCompletionBlock 完成的回调
 */
- (void)unmuteMembers:(NSArray *)aMembers
         fromChatroom:(NSString *)aChatroomId
           completion:(void (^)(EMChatroom *aChatroom, EMError *aError))aCompletionBlock;
                               
//调用                                 
EMError *error = nil;
EMChatroom *room = [[EMClient sharedClient].roomManager unmuteMembers:@[@"userName"] fromChatroom:@"roomId" error:&error];

聊天室相关的回调

注册聊天室回调

//注册聊天室回调
[[EMClient sharedClient].roomManager addDelegate:self delegateQueue:nil];
//移除聊天室回调
[[EMClient sharedClient].roomManager removeDelegate:self];
/*!
 *  有用户加入聊天室
 *
 *  @param aChatroom    加入的聊天室
 *  @param aUsername    加入者
 */
- (void)userDidJoinChatroom:(EMChatroom *)aChatroom
                       user:(NSString *)aUsername;

/*!
 *  有用户离开聊天室
 *
 *  @param aChatroom    离开的聊天室
 *  @param aUsername    离开者
 */
- (void)userDidLeaveChatroom:(EMChatroom *)aChatroom
                        user:(NSString *)aUsername;

/*!
 *  被踢出聊天室
 *
 *  @param aChatroom    被踢出的聊天室
 *  @param aReason      被踢出聊天室的原因
 */
- (void)didDismissFromChatroom:(EMChatroom *)aChatroom
                        reason:(EMChatroomBeKickedReason)aReason;

/*!
 *  有成员被加入禁言列表
 *
 *  @param aChatroom        聊天室
 *  @param aMutedMembers    被禁言的成员
 *  @param aMuteExpire      禁言失效时间,暂时不可用
 */
- (void)chatroomMuteListDidUpdate:(EMChatroom *)aChatroom
                addedMutedMembers:(NSArray *)aMutes
                       muteExpire:(NSInteger)aMuteExpire;

/*!
 *  有成员被移出禁言列表
 *
 *  @param aChatroom        聊天室
 *  @param aMutedMembers    移出禁言列表的成员
 */
- (void)chatroomMuteListDidUpdate:(EMChatroom *)aChatroom
              removedMutedMembers:(NSArray *)aMutes;

/*!
 *  有成员被加入管理员列表
 *
 *  @param aChatroom    聊天室
 *  @param aAdmin       加入管理员列表的成员
 */
- (void)chatroomAdminListDidUpdate:(EMChatroom *)aChatroom
                        addedAdmin:(NSString *)aAdmin;

/*!
 *  有成员被移出管理员列表
 *
 *  @param aChatroom    聊天室
 *  @param aAdmin       移出管理员列表的成员
 */
- (void)chatroomAdminListDidUpdate:(EMChatroom *)aChatroom
                      removedAdmin:(NSString *)aAdmin;

/*!
 *  聊天室创建者有更新
 *
 *  @param aChatroom    聊天室
 *  @param aNewOwner    新群主
 *  @param aOldOwner    旧群主
 */
- (void)chatroomOwnerDidUpdate:(EMChatroom *)aChatroom
                      newOwner:(NSString *)aNewOwner
                      oldOwner:(NSString *)aOldOwner;

上一页:群组管理

下一页:实时通话