public bool Remove(string connectionId)
        {
            lock (_syncObj)
            {
                var isRemoved   = false;
                var _database   = GetDatabase();
                var clientValue = _database.HashGet(_clientStoreKey, connectionId);
                if (clientValue.IsNullOrEmpty)
                {
                    return(isRemoved);
                }

                _database.HashDelete(_clientStoreKey, connectionId);
                isRemoved = true;

                var client = JsonConvert.DeserializeObject <OnlineClient>(clientValue);

                if (isRemoved)
                {
                    var user = client.ToUserIdentifierOrNull();

                    if (user != null && !IsUserOnline(user))
                    {
                        UserDisconnected.InvokeSafely(this, new OnlineUserEventArgs(user, client));
                    }

                    ClientDisconnected.InvokeSafely(this, new OnlineClientEventArgs(client));
                }

                return(isRemoved);
            }
        }
Beispiel #2
0
        /// <summary>
        /// 移除Client
        /// </summary>
        /// <param name="connectionId"></param>
        /// <returns></returns>
        public bool Remove(string connectionId)
        {
            lock (_syncObj)
            {
                var _database   = GetDatabase();
                var clientValue = _database.HashGet(_clientStoreKey, connectionId);
                if (clientValue.IsNullOrEmpty)
                {
                    return(true);
                }

                var client = JsonConvert.DeserializeObject <OnlineClient>(clientValue);
                var user   = client.ToUserIdentifierOrNull();
                if (user != null)
                {
                    //从_userStoreKey中移除一个client
                    var userClientsValue = _database.HashGet(_userStoreKey, user.ToUserIdentifierString());
                    if (userClientsValue.HasValue)
                    {
                        var userClients = JsonConvert.DeserializeObject <List <string> >(userClientsValue);
                        userClients.Remove(connectionId);
                        if (userClients.Count > 0)
                        {
                            //更新
                            _database.HashSet(_userStoreKey, new HashEntry[] { new HashEntry(user.ToUserIdentifierString(), userClients.ToJsonString()) });
                        }
                        else
                        {
                            //删除
                            _database.HashDelete(_userStoreKey, user.ToUserIdentifierString());
                        }
                    }

                    _database.HashDelete(_clientStoreKey, connectionId);

                    if (!IsUserOnline(user))
                    {
                        UserDisconnected.InvokeSafely(this, new OnlineUserEventArgs(user, client));
                    }
                }

                ClientDisconnected.InvokeSafely(this, new OnlineClientEventArgs(client));
                return(true);
            }
        }
Beispiel #3
0
        public bool Remove(string connectionId)
        {
            lock (_syncObj)
            {
                IOnlineClient client;
                var           isRemoved = _clients.TryRemove(connectionId, out client);

                if (isRemoved)
                {
                    var user = client.ToUserIdentifierOrNull();

                    if (user != null && !this.IsOnline(user))
                    {
                        UserDisconnected.InvokeSafely(this, new OnlineUserEventArgs(user, client));
                    }
                    ClientDisconnected.InvokeSafely(this, new OnlineClientEventArgs(client));
                }

                return(isRemoved);
            }
        }
        public virtual bool Remove(string connectionId)
        {
            lock (SyncObj)
            {
                IOnlineClient client;
                var           isRemoved = Clients.TryRemove(connectionId, out client);

                if (isRemoved)
                {
                    var user = client.ToUserIdentifierOrNull();

                    if (user != null && !this.IsOnline(user))
                    {
                        UserDisconnected.InvokeSafely(this, new OnlineUserEventArgs(user, client));
                    }

                    ClientDisconnected.InvokeSafely(this, new OnlineClientEventArgs(client));
                }
                _cacheManager.GetCache <string, IOnlineClient>(nameof(IOnlineClient)).Remove(connectionId);
                return(isRemoved);
            }
        }