我正在尝试创建一个语音会议室,所有用户都可以说话并使用麦克风。但作为管理员,我应该有权将任何用户静音。因此,我向用户添加了麦克风的属性,该属性将在客户端进行检查并相应地启用/禁用用户的麦克风。服务器端代码如下所示:
String identifier;
String userID;
private int _gId = 1;
private Map<String,Object> newUser;
@Override
public boolean appConnect(IConnection conn, Object[] params) {
identifier = (String)params[1];
userID = (String)params[0];
int _globalUserId = _gId++;
conn.getClient().setAttribute("id", _globalUserId);
newUser = new HashMap<String,Object>();
newUser.put("identifier", (String)params[0]);
newUser.put("mic", 1); //mic value to be checked in client side
return true;
}
@Override
public boolean roomJoin(IClient client, IScope scope) {
ISharedObject so = getSharedObject(scope, "users_so");
so.setAttribute(userID,newUser);
return true;
}
@SuppressWarnings("unchecked")
public void muteUser(String userID){
IScope scope = Red5.getConnectionLocal().getScope();
ISharedObject so = getSharedObject(scope, "users_so");
Map<String,Object> user= new HashMap<String,Object>();
user = (Map<String, Object>) so.getAttribute(userID);
if(user != null){
user.put("mic", 0);
so.beginUpdate();
boolean removed = so.removeAttribute(userID);
boolean updated = so.setAttribute(userID,user);
so.endUpdate();
log.info("Mic: " + user.get("mic"));
log.info("Removed: " + removed);
log.info("Updated: " + updated);
}
}
当我尝试调用muteUser方法时出现问题。 Red5表示流已关闭。我认为当我删除用户的属性并再次添加它时会发生这种情况,但我无法找到另一种方法来更新sharedObject的麦克风值。
是否有人更好地更新sharedObject而不会丢失流?
答案 0 :(得分:0)
您要求的SO并不像用户地图那样工作,您认为它会在您的示例中显示。我建议在SO中存储地图,然后对地图进行获取/添加;在这种情况下,地图是共享的,因此您必须使其成为线程安全的;我会像这样使用ConcurrentMap:
ISharedObject so = getSharedObject(scope, "users_so");
if (so == null) {
// make sure your so exists
}
so.beginUpdate();
ConcurrentMap<String, Object> users = (ConcurrentMap<String, Object>) so.getAttribute("users");
if (users == null) {
users = new ConcurrentHashMap<String, Object>();
so.setAttribute("users" users);
}
Object user = users.get(userID);
user.put("mic", 0);
so.endUpdate();