房间用户管理
本文档适用于开发以下平台应用:iOS、Android、macOS、Windows。
功能简介
ZIM SDK 支持设置房间内的用户属性,即在房间内,自定义房间内的用户属性。
可应用的场景:
- 设置成员角色,如设置房主、管理员、观众、观众等级。
- 开/关摄像头、开/关麦克风、是否能发言、是否共享白板等。
实现流程
- 每个房间中,最多允许设置 500 个用户的用户属性,以
Key-Value
的方式进行存储。开发者如果需要提高属性上限,请联系 ZEGO 技术支持。 - 房间内的每个用户,所拥有的用户属性
Key-Value
的总长度不超过 144 字节,且Key-Value
的个数不超过 30 对。单个Key-Value
的长度,Key 不超过 8 字节、Value 不超过 64 字节。开发者如果需要提高上限,请联系 ZEGO 技术支持。 - 房间销毁后,设置的自定义用户属性也会同时销毁。
设置用户属性
操作者和被设置/修改属性的用户,都必须是该房间内的用户。
开发者可以通过 setRoomMembersAttributes 接口,设置房间内用户的用户属性。设置的房间用户属性以 Key-Value
的形式存储:
- 当 Key 不存在时,设置用户属性表示增加属性。
- 当 key 已经存在时,设置用户属性表示更新已有属性的取值。
设置成功后,可以通过 ZIMRoomMembersAttributesOperatedResult 的异步返回值,获取操作结果。
HashMap<String,String> attributes = new HashMap<>();
attributes.put("key1","value1");
ArrayList<String> userIDs = new ArrayList<>();
userIDs.add("userID1");
ZIMRoomMemberAttributesSetConfig setConfig = new ZIMRoomMemberAttributesSetConfig();
setConfig.isDeleteAfterOwnerLeft = true;
ZIM.getInstance().setRoomMembersAttributes(attributes, "roomID", userIDs, setConfig, new ZIMRoomMembersAttributesOperatedCallback() {
@Override
public void onRoomMembersAttributesOperated(String roomID, ArrayList<ZIMRoomMemberAttributesOperatedInfo> infos, ArrayList<String> errorUserList, ZIMError errorInfo) {
//这里填写操作结果返回的逻辑
}
});
ZIMRoomMemberAttributesSetConfig *setConfig = [[ZIMRoomMemberAttributesSetConfig alloc] init];
[[ZIM getInstance] setRoomMembersAttributes:@{@"key1":@"value1"} userIDs:@[@"user1",@"user2"] roomID:@"roomID" config:setConfig callback:^(NSString * _Nonnull roomID, NSArray<ZIMRoomMemberAttributesOperatedInfo *> * _Nonnull operatedInfos, NSArray<NSString *> * _Nonnull errorUserList, ZIMError * _Nonnull errorInfo) {
//这里写设置结果返回后的业务逻辑
}];
Dictionary<string, string> attributes = new Dictionary<string, string>();
attributes.Add("key1", "value1");
List<string> userIDs = new List<string>();
userIDs.Add("userID1");
ZIMRoomMemberAttributesSetConfig setConfig = new ZIMRoomMemberAttributesSetConfig();
setConfig.isDeleteAfterOwnerLeft = true;
ZIM.GetInstance().SetRoomMembersAttributes(attributes, userIDs, "roomID",
setConfig,
(string roomID, List<ZIMRoomMemberAttributesOperatedInfo> infos, List<string> errorUserList, ZIMError errorInfo) =>
{
//这里填写操作结果返回的逻辑
}
);
Map<String, String> attributes = {"key1": "value1"};
List<String> userIDs = ["userID1"];
ZIM
.getInstance()
?.setRoomMembersAttributes(
attributes, userIDs, 'roomID', ZIMRoomMemberAttributesSetConfig())
.then((value) => {
//这里填写成功后的逻辑
})
.catchError((onError) {
//这里写失败后的逻辑
});
const config = {
isDeleteAfterOwnerLeft: false
};
zim.setRoomMembersAttributes({'key1': 'value1'}, ['user1', 'user2'], 'roomID', config).then(res => {
// 这里写设置结果返回后的业务逻辑
});
std::unordered_map<std::string, std::string> attributes;
attributes.emplace(std::string("key1"),std::string("value1"));
std::vector<std::string> userIDs;
userIDs.push_back(std::string("userID1"));
ZIMRoomMemberAttributesSetConfig setConfig;
setConfig.isDeleteAfterOwnerLeft = true;
ZIM::getInstance()->setRoomMembersAttributes(attributes, userIDs, std::string("roomID"), setConfig, [=](const std::string &roomID, const std::vector<ZIMRoomMemberAttributesOperatedInfo> &infos, const std::vector<std::string> &errorUsers, const ZIMError &errorInfo){
//这里填写操作结果返回的逻辑
});
查询用户属性
房间内的用户,可以通过 queryRoomMembersAttributes 接口,查询房间内某些用户的用户属性。
查询成功后,可以通过 ZIMRoomMembersAttributesQueriedResult 的异步返回值,获取查询结果。
ArrayList<String> userIDs = new ArrayList<>();
userIDs.add("userID1");
ZIM.getInstance().queryRoomMembersAttributes(userIDs, "roomID", new ZIMRoomMembersAttributesQueriedCallback() {
@Override
public void onRoomMembersAttributesQueried(String roomID, ArrayList<ZIMRoomMemberAttributesInfo> infos, ZIMError errorInfo) {
//这里填写查询结果返回的逻辑
}
});
[[ZIM getInstance] queryRoomMembersAttributesByUserIDs:@[@"userID1",@"userID2"] roomID:@"roomID" callback:^(NSString * _Nonnull roomID, NSArray<ZIMRoomMemberAttributesInfo *> * _Nonnull infos, ZIMError * _Nonnull errorInfo) {
//这里写查询结果返回后的业务逻辑
}];
List<string> userIDs = new List<string>();
userIDs.Add("userID1");
ZIM.GetInstance().QueryRoomMembersAttributes(userIDs, "roomID", (string roomID, List<ZIMRoomMemberAttributesInfo> infos, ZIMError errorInfo) =>
{
//这里填写查询结果返回的逻辑
}
);
Map<String, String> attributes = {"key1": "value1"};
List<String> userIDs = ["userID1"];
ZIM
.getInstance()
?.queryRoomMembersAttributes(userIDs, 'roomID')
.then((value) => {
//这里填写成功后的逻辑
})
.catchError((onError) {
//这里写失败后的逻辑
});
zim.queryRoomMembersAttributes(['user1', 'user2'], 'roomID').then(res => {
// 这里写查询结果返回后的业务逻辑
});
std::vector<std::string> userIDs;
userIDs.push_back(std::string("userID1"));
ZIM::getInstance()->queryRoomMembersAttributes(userIDs, std::string("roomID"), [=](const std::string &roomID, const std::vector<ZIMRoomMemberAttributesInfo> &infos,const ZIMError &errorInfo){
//这里填写查询结果返回的逻辑
});
查询所有用户属性
房间内的用户,可以通过 queryRoomMemberAttributesList 接口,查询该房间内的所有用户的用户属性。
查询成功后,会返回该房间内所有用户的用户属性。可以通过 ZIMRoomMemberAttributesListQueriedResult 的异步返回值,获取查询结果。
ZIMRoomMemberAttributesQueryConfig queryConfig = new ZIMRoomMemberAttributesQueryConfig();
queryConfig.count = 100;
queryConfig.nextFlag = "";
ZIM.getInstance().queryRoomMemberAttributesList(
"roomID", queryConfig, new ZIMRoomMemberAttributesListQueriedCallback() {
@Override
public void onRoomMemberAttributesListQueried(String roomID, ArrayList<ZIMRoomMemberAttributesInfo> infos, String nextFlag,ZIMError errorInfo) {
if (nextFlag == "") {
//所有用户的房间用户属性均已查完
}else{
//继续查询下一分页
}
}
});
ZIMRoomMemberAttributesQueryConfig *queryConfig = [[ZIMRoomMemberAttributesQueryConfig alloc] init];
queryConfig.count = 100;
[[ZIM getInstance] queryRoomMemberAttributesListByRoomID:@"roomID" config:queryConfig callback:^(NSString * _Nonnull roomID, NSArray<ZIMRoomMemberAttributesInfo *> * _Nonnull infos, NSString * _Nonnull nextFlag, ZIMError * _Nonnull errorInfo) {
//这里写查询结果返回后的业务逻辑
}];
ZIMRoomMemberAttributesQueryConfig queryConfig = new ZIMRoomMemberAttributesQueryConfig();
queryConfig.count = 100;
queryConfig.nextFlag = "";
ZIM.GetInstance().QueryRoomMemberAttributesList(
"roomID", queryConfig, (string roomID, List<ZIMRoomMemberAttributesInfo> infos, string nextFlag, ZIMError errorInfo) =>
{
if (nextFlag == "")
{
//所有用户的房间用户属性均已查完
}
else
{
//继续查询下一分页
}
}
);
ZIM
.getInstance()
?.queryRoomMemberAttributesList(
'roomID', ZIMRoomMemberAttributesQueryConfig())
.then((value) => {
//这里填写成功后的逻辑
})
.catchError((onError) {
//这里写失败后的逻辑
});
const config = {
nextFlag: '',
count: 100
}
zim.queryRoomMemberAttributesList('roomID', config).then(res => {
// 这里写查询结果返回后的业务逻辑
});
ZIMRoomMemberAttributesQueryConfig queryConfig = ZIMRoomMemberAttributesQueryConfig(std::string(""), 100);
ZIM::getInstance()->queryRoomMemberAttributesList(std::string("roomID"), queryConfig, [=](const std::string &roomID, const std::vector<ZIMRoomMemberAttributesInfo> &infos,const std::string &nextFlag, const ZIMError &errorInfo){
if (nextFlag == "") {
//所有用户的房间用户属性均已查完
}else{
//继续查询下一分页
}
});
用户属性变更通知
当房间内成员的房间用户属性发生变更时,开发者可以通过注册 on 的回调接口 onRoomMemberAttributesUpdated 方法,监听房间内成员的用户属性变更通知。
public void onRoomMemberAttributesUpdated(ZIM zim,
ArrayList<ZIMRoomMemberAttributesUpdateInfo> infos,
ZIMRoomOperatedInfo operatedInfo, String roomID) {
//这里写房间用户属性发生变更后的业务逻辑
}
- (void)zim:(ZIM *)zim
roomMemberAttributesUpdated:(NSArray<ZIMRoomMemberAttributesUpdateInfo * > *)updateInfo
operatedUserInfo:(ZIMRoomOperatedInfo *)operatedInfo
roomID:(NSString *)roomID{
//这里写房间用户属性发生变更后的业务逻辑
}
ZIM.GetInstance().onRoomMemberAttributesUpdated = (
ZIM zim, List<ZIMRoomMemberAttributesUpdateInfo> infos,
ZIMRoomOperatedInfo operatedInfo, string roomID) =>
{
//这里写房间用户属性发生变更后的业务逻辑
};
ZIMEventHandler.onRoomMemberAttributesUpdated = (
ZIM zim,List<ZIMRoomMemberAttributesUpdateInfo> infos,ZIMRoomOperatedInfo operatedInfo, String roomID){
//这里写房间用户属性发生变更后的业务逻辑
};
zim.on('roomMemberAttributesUpdated', (zim, data) => {
// 这里写房间用户属性发生变更后的业务逻辑
});
void onRoomMemberAttributesUpdated(ZIM * /*zim*/, const std::vector<ZIMRoomMemberAttributesUpdateInfo> & /*infos*/,ZIMRoomOperatedInfo /*operatedInfo*/, const std::string & /*roomID*/) override{
//这里写房间用户属性发生变更后的业务逻辑
}
在房间内,如果有用户调用了 setRoomMembersAttributes 接口时:
- 当房间内人数在 500 人以内时,所有房间内的用户,均可以收到房用户属性变更的通知。
- 当房间内人数超过 500 人时,仅操作者和被设置/修改属性的用户,可以收到房用户属性变更的通知。
开发者如果需要提高上限,请联系 ZEGO 技术支持。
批量查询房间用户信息
开发者可以通过调用 queryRoomMembers 接口,传入 roomID 、 userID 数组,批量查询房间中用户的信息。接口调用成功后, 会返回查询成功的 userList(可用于确认目标用户在房间中),以及查询失败的 errorUserList(可用于确认目标用户不在房间中)。
批量查询用户数量的默认上限为 10 名。
示例代码如下所示:
String targetUserID = "targetUserIdA";
ArrayList<String> userIDs = new ArrayList<>();
userIDs.add(targetUserID);
// userID 映射 ZIMRoomMemberInfo 的 map,可以缓存起来用于后续 UI 展示,目前 ZIMRoomMemberInfo 暴露了 userID , userName。
HashMap<String,ZIMRoomMemberInfo> roomMemberMap = new HashMap<>();
ZIM.getInstance().queryRoomMembers(userIDs, "targetRoomID", new ZIMRoomMembersQueriedCallback() {
@Override
public void onRoomMembersQueried(String roomID, ArrayList<ZIMRoomMemberInfo> memberList, ArrayList<ZIMErrorUserInfo> errorUserList, ZIMError errorInfo) {
if(errorInfo.code != ZIMErrorCode.SUCCESS){
Log.d("ZIM CallBack","query room members error! error code:"+errorInfo.code+", error message:"+errorInfo.message);
}
HashMap<String,ZIMErrorUserInfo> targetErrorUserInfoMap = new HashMap<>();
for (ZIMRoomMemberInfo memberInfo:
memberList) {
roomMemberMap.put(memberInfo.userID,memberInfo);
}
// 针对查询失败的部分 userID, 映射 ZIMErrorUserInfo 的 map, 如果有需要关注失败原因可以读取此数据结构
for (ZIMErrorUserInfo errorUserInfo:
errorUserList) {
targetErrorUserInfoMap.put(errorUserInfo.userID,errorUserInfo);
}
// 判断是否目标成员是否在房间内
boolean isTargetUserIdA = roomMemberMap.containsKey(targetUserID);
}
});
NSString *userIdA = @"targetUserIdA";
NSString *userIdB = @"targetUserIdB";
NSArray<NSString *> *targetUserIDs = @[userIdA,userIdB];
// userID 映射 ZIMRoomMemberInfo 的 map, 可以缓存起来用于后续 UI 展示,目前 ZIMRoomMemberInfo 暴露了 userID , userName。
NSMutableDictionary *targetRoomMemberMap = [[NSMutableDictionary alloc] init];
[[ZIM getInstance] queryRoomMembersByUserIDs:targetUserIDs roomID:@"targetRoomID" callback:^(NSString * _Nonnull roomID, NSArray<ZIMRoomMemberInfo *> * _Nonnull memberList, NSArray<ZIMErrorUserInfo *> * _Nonnull errorUserList, ZIMError * _Nonnull errorInfo) {
if(errorInfo.code != ZIMErrorCodeSuccess){
NSLog(@"query room members failed! error code:%ld, error message: %@",errorInfo.code, errorInfo.message);
return;
}
// userID 映射 ZIMErrorUserInfo 的 map, 如果有需要关注失败原因可以读取此数据结构
NSMutableDictionary *targetErrorUserInfoMap = [[NSMutableDictionary alloc] init];
for (ZIMRoomMemberInfo *currentMemberInfo in memberList) {
[targetRoomMemberMap setObject:currentMemberInfo forKey:currentMemberInfo.userID];
}
for (ZIMErrorUserInfo *currentErrorUserInfo in errorUserList){
[targetErrorUserInfoMap setObject:currentErrorUserInfo forKey:currentErrorUserInfo.userID];
}
// 判断是否目标成员是否在房间内
bool isTargetUserIdAInTheRoom = [[targetRoomMemberMap allKeys] containsObject:userIdA];
}];
string userIdA = "UserIdA";
string userIdB = "UserIdB";
List<string> userIDs = new List<string>();
userIDs.Add(userIdA);
userIDs.Add(userIdB);
ZIM.GetInstance().QueryRoomMembers(userIDs, "roomID", (string roomID, List<ZIMRoomMemberInfo> memberList, List<ZIMErrorUserInfo> errorUserList, ZIMError errorInfo)=>{
if (errorInfo.code == ZIMErrorCode.Success){
//调用成功
}
});
ZIM.getInstance().queryRoomMembers(["userA","userB"], "roomID")
.then((ZIMRoomMembersQueriedResult value) {
// userID 映射 ZIMErrorUserInfo 的 map, 如果有需要关注失败原因可以读取此数据结构
Map<String, ZIMErrorUserInfo> targetErrorUserInfoMap = {};
Map<String, ZIMRoomMemberInfo> targetRoomMemberMap = {};
for (ZIMRoomMemberInfo currentMemberInfo in value.memberList) {
targetRoomMemberMap[currentMemberInfo.userID] = currentMemberInfo;
}
for (ZIMErrorUserInfo currentErrorUserInfo in value.errorUserList) {
targetErrorUserInfoMap[currentErrorUserInfo.userID] = currentErrorUserInfo;
}
// 判断是否目标成员是否在房间内
bool isTargetUserIdAInTheRoom = targetRoomMemberMap.containsKey(userIdA);
}).catchError((onError){
// 处理失败
});
const userIDs = ['user1', 'user2'];
zim.queryRoomMembers(userIDs, 'roomID').then(res => {
// 这里写查询结果返回后的业务逻辑
});
std::vector<std::string> targetUserIDs;
ZIM::getInstance().queryRoomMembers(targetUserIDs, roomID, [targetRoomMemberMap](const std::string &roomID, const std::vector<ZIMRoomMemberInfo> &memberList,const std::vector<ZIMErrorUserInfo> &errorUserList, const ZIMError &errorInfo) {
if (errorInfo.code != ZIMErrorCodeSuccess) {
std::cout << "Query room members failed! error code: " << errorInfo.code << ", error message: " << errorInfo.message << std::endl;
return;
}
// userID 映射 ZIMRoomMemberInfo 的 map, 可以缓存起来用于后续 UI 展示,目前 ZIMRoomMemberInfo 暴露了 userID , userName。
std::map<std::string, ZIMRoomMemberInfo> targetRoomMemberMap;
// userID 映射 ZIMErrorUserInfo 的 map, 如果有需要关注失败原因可以读取此数据结构
std::map<std::string, ZIMErrorUserInfo> targetErrorUserInfoMap;
for (auto const& currentMemberInfo : memberList) {
targetRoomMemberMap[currentMemberInfo.userID] = currentMemberInfo;
}
for (auto const& currentErrorUserInfo : errorUserList) {
targetErrorUserInfoMap[currentErrorUserInfo.userID] = currentErrorUserInfo;
}
// 判断是否目标成员是否在房间内
bool isTargetUserIdAInTheRoom = std::find(targetRoomMemberMap.begin(), targetRoomMemberMap.end(), userIdA) != targetRoomMemberMap.end();
});