Ejemplo n.º 1
0
    public void Join(string userName)
    {
        lock (ParticipantsConnectionLock)
        {
            var userId = Context.User.GetClaim(OpenIdConnectConstants.Claims.Subject);
            AllConnectedParticipants.Add(new ChatParticipantViewModel()
            {
                DisplayName  = userName,
                Email        = Context.User.Identity.Name,
                Status       = EnumChatParticipantStatus.Online,
                UserId       = userId,
                HubContextId = Context.ConnectionId
            });

            var chatGrooup = this._ctx.Participants.Include(d => d.User)
                             .Join(
                this._ctx.Participants.Include(d => d.User)
                , p1 => p1.Group.Id, p2 => p2.Group.Id,
                (p1, p2) => new { p2, p1 })
                             .Join(
                AllConnectedParticipants,
                p => p.p2.User.Id, cp => cp.UserId,
                (p, cp) => new { p2 = p.p2, p1 = p.p1, HubContextId = cp.HubContextId }
                )
                             .Where(d => d.p2.Id != d.p1.Id && d.p1.User.Id == userId)
                             .Select(d => d.HubContextId);

            // This will be used as the user's unique ID to be used on ng-chat as the connected user.
            // You should most likely use another ID on your application
            Clients.Caller.SendAsync("generatedUserId", Context.ConnectionId);


            Clients.Clients(chatGrooup.Select(d => d).ToArray()).SendAsync("friendsListChanged", AllConnectedParticipants.Last());
        }
    }