public void AddLoginUser(User user, IMClientPeer peer) { //重复登录 if (user == peer.LoginUser) { return; } //同一个peer切换用户 if (peer.LoginUser != null) { if (mLoginUsers.ContainsKey(peer.LoginUser.Username)) { mLoginUsers.Remove(peer.LoginUser.Username); } } if (mLoginUsers.TryGetValue(user.Username, out var oldPeer)) { //重复登录 if (oldPeer == peer) { return; } //旧的挤掉线 mLogger.InfoFormat("挤掉线。旧客户端:{0},新客户端:{1},用户名:{2}", oldPeer, peer, user.Username); oldPeer.Disconnect(); mLoginUsers[user.Username] = peer; } else { mLoginUsers.Add(user.Username, peer); } peer.LoginUser = user; }
public void RemoveLoginUser(IMClientPeer peer) { if (peer.LoginUser == null) { return; } string username = peer.LoginUser.Username; if (string.IsNullOrEmpty(username)) { return; } //现在登陆的和掉线的不是同一个客户端 if (!mLoginUsers.TryGetValue(username, out var nowPeer) || nowPeer != peer) { return; } mLoginUsers.Remove(username); }
public bool TryGetPeerByUsername(string username, out IMClientPeer peer) { return(mLoginUsers.TryGetValue(username, out peer)); }