Пример #1
0
        public override async Task OnConnectedAsync()
        {
            IHubUserModel user = new HubUserModel
            {
                Id = Context.User.Claims.First(i => i.Type == ClaimTypes.Name).Value.ToInt(0)
            };

            user.ConnectionId.Add(Context.ConnectionId);

            await base.OnConnectedAsync().ConfigureAwait(false);

            var addNewUser = await HubConnectedUserStore.Add(user).ConfigureAwait(false);

            await Groups.AddToGroupAsync(Context.ConnectionId, "connection_" + Context.ConnectionId).ConfigureAwait(false);

            await Groups.AddToGroupAsync(Context.ConnectionId, "user_" + user.Id).ConfigureAwait(false);

            var connectedUsersList = await HubConnectedUserStore.ConnectedUserList().ConfigureAwait(false);

            await Clients.Group("connection_" + Context.ConnectionId).SendAsync("GiveConnectedUsersList", connectedUsersList).ConfigureAwait(false);

            if (addNewUser)
            {
                await Clients.All.SendAsync("ConnectedNewClient", user).ConfigureAwait(false);
            }
        }
Пример #2
0
        public override async Task OnDisconnectedAsync(Exception exception)
        {
            var user = await HubConnectedUserStore.FindUser(Context.ConnectionId).ConfigureAwait(false);

            await base.OnDisconnectedAsync(exception).ConfigureAwait(false);

            var removeUser = await HubConnectedUserStore.Remove(Context.ConnectionId).ConfigureAwait(false);

            await Groups.RemoveFromGroupAsync(Context.ConnectionId, "connection_" + Context.ConnectionId).ConfigureAwait(false);

            await Groups.RemoveFromGroupAsync(Context.ConnectionId, "user_" + user.Id).ConfigureAwait(false);

            if (removeUser)
            {
                user.ConnectionId.Clear();
                await Clients.All.SendAsync("DisconnectedClient", user).ConfigureAwait(false);
            }
        }
Пример #3
0
 public async Task <IList <IHubUserModel> > GetConnectedUsersList()
 {
     return(await HubConnectedUserStore.ConnectedUserList().ConfigureAwait(false));
 }