protected override async Task OnClientConnectedAsync(IOnlineClient client)
        {
            await base.OnClientConnectedAsync(client);

            // 加入通讯组
            var userGroups = await UserGroupStore.GetUserGroupsAsync(client.TenantId, client.UserId.Value);

            foreach (var group in userGroups)
            {
                await Groups.AddToGroupAsync(client.ConnectionId, group.Name);

                var groupClient = Clients.Group(group.Name);
                if (groupClient != null)
                {
                    // 发送用户上线通知
                    await groupClient.SendAsync("onUserOnlined", client.TenantId, client.UserId.Value);
                }
            }

            // 发送好友上线通知
            var userFriends = await FriendStore.GetListAsync(client.TenantId, client.UserId.Value);

            if (userFriends.Count > 0)
            {
                var friendClientIds = userFriends.Select(friend => friend.FriendId.ToString()).ToImmutableArray();
                var userClients     = Clients.Users(friendClientIds);
                if (userClients != null)
                {
                    await userClients.SendAsync("onUserOnlined", client.TenantId, client.UserId.Value);
                }
            }
        }
Exemplo n.º 2
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()) });
        }
 /// <summary>
 /// 添加客户端
 /// </summary>
 /// <param name="client"></param>
 public void Add(IOnlineClient client)
 {
     lock (SyncObj)
     {
         Clients[client.ConnectionId] = client;
     }
 }
        public override async Task OnConnectedAsync()
        {
            await base.OnConnectedAsync();

            IOnlineClient onlineClient = CreateClientForCurrentConnection();

            await OnConnectedAsync(onlineClient);
        }
 protected virtual async Task OnClientDisconnectedAsync(IOnlineClient client)
 {
     // 角色添加进组
     foreach (var role in client.Roles)
     {
         await Groups.RemoveFromGroupAsync(client.ConnectionId, role);
     }
 }
        public static bool Remove(
            [NotNull] this IOnlineClientManager onlineClientManager,
            [NotNull] IOnlineClient client)
        {
            Check.NotNull(onlineClientManager, nameof(onlineClientManager));
            Check.NotNull(client, nameof(client));

            return(onlineClientManager.Remove(client.ConnectionId));
        }
        public override async Task OnConnectedAsync()
        {
            await base.OnConnectedAsync();

            IOnlineClient onlineClient = CreateClientForCurrentConnection();

            Logger.LogDebug("A client is connected: " + onlineClient.ToString());
            OnlineClientManager.Add(onlineClient);
        }
Exemplo n.º 8
0
        private dynamic GetSignalRClientOrNull(IOnlineClient client)
        {
            var signalRClient = ChatHub.Clients.Client(client.ConnectionId);

            if (signalRClient == null)
            {
                Logger.Debug("Can not get chat user " + client.UserId + " from SignalR hub!");
                return(null);
            }
            return(signalRClient);
        }
Exemplo n.º 9
0
        protected override async Task OnClientDisconnectedAsync(IOnlineClient client)
        {
            await base.OnClientDisconnectedAsync(client);

            if (client.TenantId.HasValue)
            {
                // 以租户为分组,将移除租户通讯组
                await Groups.RemoveFromGroupAsync(client.ConnectionId, client.TenantId.Value.ToString(), Context.ConnectionAborted);
            }
            else
            {
                await Groups.RemoveFromGroupAsync(client.ConnectionId, "Global", Context.ConnectionAborted);
            }
        }
Exemplo n.º 10
0
        public override async Task OnConnectedAsync()
        {
            await base.OnConnectedAsync();

            IOnlineClient onlineClient = CreateClientForCurrentConnection();

            Logger.LogDebug("A client is connected: " + onlineClient.ToString());
            OnlineClientManager.Add(onlineClient);
            if (onlineClient.TenantId.HasValue)
            {
                // 以租户为分组,将用户加入租户通讯组
                await Groups.AddToGroupAsync(onlineClient.ConnectionId, onlineClient.TenantId.Value.ToString());
            }
        }
Exemplo n.º 11
0
        public override async Task OnConnectedAsync()
        {
            await base.OnConnectedAsync();

            IOnlineClient onlineClient = CreateClientForCurrentConnection();

            Logger.LogDebug("A client is connected: " + onlineClient.ToString());
            OnlineClientManager.Add(onlineClient);
            // 加入通讯组
            var userGroups = await UserGroupStore.GetUserGroupsAsync(onlineClient.TenantId, onlineClient.UserId.Value);

            foreach (var group in userGroups)
            {
                await Groups.AddToGroupAsync(onlineClient.ConnectionId, group.Name);
            }
        }
 public virtual async Task OnDisconnectedAsync(IOnlineClient client)
 {
     if (client != null)
     {
         try
         {
             Logger.LogDebug("A client is disconnected: " + client);
             // 移除在线客户端
             OnlineClientManager.Remove(Context.ConnectionId);
             await OnClientDisconnectedAsync(client);
         }
         catch (Exception ex)
         {
             Logger.LogWarning(ex.ToString(), ex);
         }
     }
 }
Exemplo n.º 13
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.ToUserIdentifier();

                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 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 (SyncObj)
            {
                var userWasAlreadyOnline = false;
                var context = client.ToClientContextOrNull();

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

                Store.Add(client);

                ClientConnected?.Invoke(this, new OnlineClientEventArgs(client));

                if (context != null && !userWasAlreadyOnline)
                {
                    UserConnected?.Invoke(this, new OnlineUserEventArgs(context, client));
                }
            }
        }
Exemplo n.º 17
0
        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));
                }
            }
        }
 public void Add(IOnlineClient client)
 {
     Clients.AddOrUpdate(client.ConnectionId, client, (s, o) => client);
 }
 public virtual async Task OnConnectedAsync(IOnlineClient client)
 {
     Logger.LogDebug("A client is connected: " + client.ToString());
     OnlineClientManager.Add(client);
     await OnClientConnectedAsync(client);
 }
Exemplo n.º 20
0
 public static UserIdentifier ToUserIdentifierOrNull(this IOnlineClient onlineClient)
 {
     return(!string.IsNullOrWhiteSpace(onlineClient.UserId)
         ? new UserIdentifier(onlineClient.UserId)
         : null);
 }
Exemplo n.º 21
0
 public static UserIdentifier ToUserIdentifierOrNull(this IOnlineClient onlineClient)
 {
     return(onlineClient.UserId.HasValue
         ? new UserIdentifier(onlineClient.TenantId, onlineClient.UserId.Value)
         : null);
 }
Exemplo n.º 22
0
 /// <summary>
 /// 构造函数
 /// </summary>
 /// <param name="client">客户端</param>
 public OnlineClientEventArgs(IOnlineClient client)
 {
     Client = client;
 }
Exemplo n.º 23
0
 public OnlineUserEventArgs(OnlineClientContext context, IOnlineClient client)
     : base(client)
 {
     Context = context;
 }
Exemplo n.º 24
0
 public static OnlineClientContext ToClientContextOrNull(this IOnlineClient onlineClient)
 {
     return(onlineClient.UserId.HasValue
         ? new OnlineClientContext(onlineClient.TenantId, onlineClient.UserId.Value)
         : null);
 }
 public bool TryGet(string connectionId, out IOnlineClient client)
 {
     return(Clients.TryGetValue(connectionId, out client));
 }
 public static bool Remove(this IOnlineClientManager onlineClientManager, IOnlineClient client)
 {
     return onlineClientManager.Remove(client.ConnectionId);
 }
 public OnlineUserEventArgs(UserIdentifier user,IOnlineClient client) 
     : base(client)
 {
     User = user;
 }
Exemplo n.º 28
0
 public bool Remove(IOnlineClient client)
 {
     return(Remove(client.ConnectionId));
 }
Exemplo n.º 29
0
 public static bool Remove(this IOnlineClientManager onlineClientManager, IOnlineClient client)
 {
     return(onlineClientManager.Remove(client.ConnectionId));
 }
 public OnlineClientEventArgs(IOnlineClient client)
 {
     Client = client;
 }
Exemplo n.º 31
0
 public Task SendOnlineClientToUser(IUserIdentifier userId, IOnlineClient onlineClient)
 {
     return(Task.CompletedTask);
 }
 public bool TryRemove(string connectionId, out IOnlineClient client)
 {
     return(Clients.TryRemove(connectionId, out client));
 }
Exemplo n.º 33
0
 public void Add(IOnlineClient client)
 {
     _clients[client.ConnectionId] = client;
 }
 /// <summary>
 /// 添加客户端
 /// </summary>
 /// <param name="client">/// </summary></param>
 public void Add(IOnlineClient client)
 {
     _clients[client.ConnectionId] = client;
 }
 public OnlineUserEventArgs(UserIdentifier user, IOnlineClient client)
     : base(client)
 {
     User = user;
 }
 /// <summary>
 /// 删除指定的客户端
 /// </summary>
 /// <param name="client">/// </summary></param>
 /// <returns></returns>
 public bool Remove(IOnlineClient client)
 {
     return Remove(client.ConnectionId);
 }