iOS

聊天室队列服务

更新时间: 2024/03/14 17:08:39

聊天室队列

聊天室提供队列服务,可结合互动直播连麦场景中的麦位管理使用。权限由 NIMChatroom - queueModificationLevel 决定,可以通过updateChatroomInfo进行修改。默认情况下,所有用户都可以调用接口修改队列内容。

获取聊天室队列

objc@protocol NIMChatroomManager <NSObject>
/**
 *  获取聊天室队列
 *
 *  @param roomId     聊天室ID
 *  @param completion 请求回调
 */
- (void)fetchChatroomQueue:(NSString *)roomId
                completion:(nullable NIMChatroomQueueInfoHandler)completion;
@end                      

示例

objcNSString *roomId = @"your_room_id";
[[NIMSDK sharedSDK].chatroomManager fetchChatroomQueue:roomId 
                                            completion:^(NSError * _Nullable error, NSArray<NSDictionary<NSString *,NSString *> *> * _Nullable info) {
    if (!error)
    {
        //deal with info
    }
    else
    {
        //deal with error
    }
}];

加入或者更新队列元素

若该元素由用户A添加,之后再由用户B更新,则该元素的所有者为用户B。

objc@protocol NIMChatroomManager <NSObject>
/**
 *  加入或者更新聊天室通用队列元素,权限由 NIMChatroom 的 queueModificationLevel 决定。
 *  
 *  @param request    聊天室队列请求
 *  @param completion 请求回调
 */
- (void)updateChatroomQueueObject:(NIMChatroomQueueUpdateRequest *)request
                       completion:(nullable NIMChatroomHandler)completion;
@end                      

NIMChatroomQueueUpdateRequest 参数列表

参数
类型
说明
roomId NSString 聊天室 ID
key NSString 更新元素的 Key 值,如果 key 在队列中存在则更新,不存在则放到队列尾部 ,长度限制为 32 字节
value NSString 更新元素的 Value 值,长度限制为 4096 字节
transient BOOL 当提交这个新元素到聊天室队列的用户从聊天室掉线或退出的时候,是否需要删除这个元素,默认为 false 不删除。当设置为 YES 时,掉线或退出后,聊天室会发出队列批量元素变更通知 NIMChatroomEventTypeQueueBatchChange
elementAccid NSString 队列元素所属账号,默认不传表示队列元素属于当前操作人,管理员可以指定队列元素归属于其他合法账号

示例

objcNIMChatroomQueueUpdateRequest *request = [[NIMChatroomQueueUpdateRequest alloc] init];
request.roomId = @"your_room_id";
request.key    = @"key";
request.value  = @"value";
request.transient = YES;
req.element_accid_ = UTF8(request.elementAccid);
[[NIMSDK sharedSDK].chatroomManager updateChatroomQueueObject:request 
                                                   completion:^(NSError * _Nullable error) {
    if (!error)
    {
        //deal with success
    }
    else
    {
        //deal with error
    }
}];

批量更新队列元素

若该元素由用户A添加,之后再由用户B更新,则该元素的所有者为用户B。

objc@protocol NIMChatroomManager <NSObject>
/**
 *  批量更新聊天室通用队列元素,权限由 NIMChatroom 的 queueModificationLevel 决定
 *
 *  @param request    聊天室队列批量请求
 *  @param completion 请求回调
 */
- (void)batchUpdateChatroomQueueObject:(NIMChatroomQueueBatchUpdateRequest *)request
                       completion:(nullable NIMChatroomQueueBatchUpdateHandler)completion;
@end                      

NIMChatroomQueueBatchUpdateRequest 参数列表

参数
类型
说明
roomId NSString 聊天室 ID
elements NSDictionary 批量更新元素的key-value对,key/value分别是elementKey和elementValue(elementKey限制128字节,elementValue限制4096字节),一次最多更新100个
needNotify BOOL 是否需要发送广播通知
notifyExt NSString 通知中的自定义字段,长度限制 2048 字节

示例

objcNIMChatroomQueueBatchUpdateRequest *request = [[NIMChatroomQueueBatchUpdateRequest alloc] init];
request.roomId = @"your_room_id";
request.needNotify = YES;
request.notifyExt = @"你收到一个通知";
request.elements = dict;
[[NIMSDK sharedSDK].chatroomManager batchUpdateChatroomQueueObject:request completion:^(NSError * _Nullable error, NSArray<NSString *> * _Nullable elements) {
    if (!error)
    {
        //deal with success
    }
    else
    {
        //deal with error
    }
}];

移除指定队列元素

objc@protocol NIMChatroomManager <NSObject>
/**
 *  移除聊天室队列元素,权限由 NIMChatroom 的 queueModificationLevel 决定
 *
 *  @param request    拉取请求
 *  @param completion 请求回调
 */
- (void)removeChatroomQueueObject:(NIMChatroomQueueRemoveRequest *)request
                       completion:(nullable NIMChatroomQueueRemoveHandler)completion;
@end                      

NIMChatroomQueueRemoveRequest 参数列表

参数 类型 说明
roomId NSString 聊天室 ID
key NSString 指定的元素的Key值,如果是第一个元素,则传空

示例

objcNIMChatroomQueueRemoveRequest *request = [[NIMChatroomQueueRemoveRequest alloc] init];
request.roomId = @"your_room_id";
request.key    = @"key";
[[NIMSDK sharedSDK].chatroomManager removeChatroomQueueObject:request completion:^(NSError * _Nullable error, NSDictionary<NSString *,NSString *> * _Nullable element) {
    if (!error)
    {
        //deal with removed element
    }
    else
    {
        //deal with error
    }
}];

清空队列

objc@protocol NIMChatroomManager <NSObject>
/**
 *  删除聊天室队列,权限由 NIMChatroom 的 queueModificationLevel 决定
 *
 *  @param roomId     聊天室ID
 *  @param completion 请求回调
 */
- (void)dropChatroomQueue:(NSString *)roomId
               completion:(nullable NIMChatroomHandler)completion;
@end                      

示例

objcNSString *roomId = @"your_room_id";
[[NIMSDK sharedSDK].chatroomManager dropChatroomQueue:roomId completion:^(NSError * _Nullable error) {
    if (!error)
    {
        //deal with success
    }
    else
    {
        //deal with error
    }
}];
此文档是否对你有帮助?
有帮助
去反馈
  • 聊天室队列
  • 获取聊天室队列
  • 加入或者更新队列元素
  • 批量更新队列元素
  • 移除指定队列元素
  • 清空队列