예제 #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());
        }
    }
예제 #2
0
        public void Join(string userName)
        {
            lock (ParticipantsConnectionLock)
            {
                var oldConnectedParticipants = AllConnectedParticipants.Where(x => x.Participant.Id == Context.UserIdentifier);

                if (oldConnectedParticipants.Count() == 0)
                {
                    AllConnectedParticipants.Add(new ParticipantResponseViewModel()
                    {
                        Metadata = new ParticipantMetadataViewModel()
                        {
                            TotalUnreadMessages = 0
                        },
                        Participant = new ChatParticipantViewModel()
                        {
                            DisplayName = userName,
                            Id          = Context.UserIdentifier
                        }
                    });
                }

                // 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.UserIdentifier);

                Clients.All.SendAsync("friendsListChanged", AllConnectedParticipants);
            }
        }
예제 #3
0
    public override Task OnConnectedAsync()
    {
        lock (ParticipantsConnectionLock)
        {
            var connectionIndex = DisconnectedParticipants.FindIndex(x => x.HubContextId == Context.ConnectionId);

            if (connectionIndex >= 0)
            {
                var participant = DisconnectedParticipants.ElementAt(connectionIndex);

                DisconnectedParticipants.Remove(participant);
                AllConnectedParticipants.Add(participant);

                Clients.All.SendAsync("friendsListChanged", AllConnectedParticipants);
            }

            return(base.OnConnectedAsync());
        }
    }
예제 #4
0
        public void GroupCreated(GroupChatParticipantViewModel group)
        {
            AllGroupParticipants.Add(group);

            // Pushing the current user to the "chatting to" list to keep track of who's created the group as well.
            // In your application you'll probably want a more sofisticated group persistency and management
            group.ChattingTo.Add(new ChatParticipantViewModel()
            {
                Id = Context.ConnectionId
            });

            AllConnectedParticipants.Add(new ParticipantResponseViewModel()
            {
                Metadata = new ParticipantMetadataViewModel()
                {
                    TotalUnreadMessages = 0
                },
                Participant = group
            });

            Clients.All.SendAsync("friendsListChanged", AllConnectedParticipants);
        }