示例#1
0
        public override async Task OnDisconnectedAsync(Exception exception)
        {
            var userId    = Guid.Parse(Context.User.Identity.Name ?? string.Empty);
            var channelId = Context.User.FindFirst("ChannelId").Value;

            var user = await _applicationDbContext.Users.SingleOrDefaultAsync(u => u.Id == userId);

            user.Connections--;

            var hostConnectionId = _hostConnectionRepository.GetConnection(channelId);

            if (user.Connections == 0 && hostConnectionId != null)
            {
                await _hostHubContext.SendUserDisconnected(hostConnectionId, userId);

                _userPermissionRepository.RemoveUser(userId);
            }

            await _applicationDbContext.SaveChangesAsync();

            await Groups.RemoveFromGroupAsync(Context.ConnectionId, channelId);
        }