Esempio n. 1
0
        private void AddClientToRedisStore(IOnlineClient client)
        {
            var _database = GetDatabase();

            _database.HashSet(_clientStoreKey, new HashEntry[] { new HashEntry(client.ConnectionId, client.ToString()) });
            var userId = client.ToUserIdentifierOrNull();

            if (userId == null)
            {
                return;
            }

            var userClients      = new List <string>();
            var userClientsValue = _database.HashGet(_userStoreKey, userId.ToUserIdentifierString());

            if (userClientsValue.HasValue)
            {
                userClients = JsonConvert.DeserializeObject <List <string> >(userClientsValue);
            }

            if (userClients.Contains(client.ConnectionId))
            {
                return;
            }

            userClients.Add(client.ConnectionId);
            _database.HashSet(_userStoreKey, new HashEntry[] { new HashEntry(userId.ToUserIdentifierString(), userClients.ToJsonString()) });
        }
Esempio n. 2
0
        /// <summary>
        /// 添加Client
        /// </summary>
        /// <param name="client"></param>
        public void Add(IOnlineClient client)
        {
            lock (_syncObj)
            {
                var userWasAlreadyOnline = false;
                var user = client.ToUserIdentifierOrNull();

                if (user != null)
                {
                    userWasAlreadyOnline = IsUserOnline(user);
                }

                AddClientToRedisStore(client);

                ClientConnected.InvokeSafely(this, new OnlineClientEventArgs(client));

                if (user != null && !userWasAlreadyOnline)
                {
                    UserConnected.InvokeSafely(this, new OnlineUserEventArgs(user, client));
                }
            }
        }
        public void Add(IOnlineClient client)
        {
            lock (_syncObj)
            {
                var userWasAlreadyOnline = false;
                var user = client.ToUserIdentifierOrNull();

                if (user != null)
                {
                    userWasAlreadyOnline = this.IsOnline(user);
                }

                _clients[client.ConnectionId] = client;

                ClientConnected.InvokeSafely(this, new OnlineClientEventArgs(client));

                if (user != null && !userWasAlreadyOnline)
                {
                    UserConnected.InvokeSafely(this, new OnlineUserEventArgs(user, client));
                }
            }
        }
        public virtual void Add(IOnlineClient client)
        {
            lock (this.SyncObj)
            {
                var userWasAlreadyOnline = false;
                var user = client.ToUserIdentifierOrNull();

                if (user != null)
                {
                    userWasAlreadyOnline = this.IsOnline(user);
                }

                this.Clients[client.ConnectionId] = client;

                this.ClientConnected.InvokeSafely(this, new OnlineClientEventArgs(client));

                if (user != null && !userWasAlreadyOnline)
                {
                    this.UserConnected.InvokeSafely(this, new OnlineUserEventArgs(user, client));
                }
            }
        }
        public virtual void Add(IOnlineClient client)
        {
            lock (SyncObj)
            {
                var userWasAlreadyOnline = false;
                var user = client.ToUserIdentifierOrNull();

                if (user != null)
                {
                    userWasAlreadyOnline = this.IsOnline(user);
                }

                Clients[client.ConnectionId] = client;
                _cacheManager.GetCache <string, IOnlineClient>(nameof(IOnlineClient))
                .Set(client.ConnectionId, client);
                ClientConnected.InvokeSafely(this, new OnlineClientEventArgs(client));

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