示例#1
0
        /// <summary>
        /// Connected handler
        /// Switches user to different pool for connected users and updates his status to online
        /// </summary>
        /// <param name="exception"></param>
        /// <returns></returns>
        public override async Task OnConnectedAsync()
        {
            var userId = Context.User.Identity.Name;                                       // fetch id

            DisconnectedParticipants.TryGetValue(userId, out var disconnectedParticipant); // get from disconnected participants
            AllConnectedParticipants.TryGetValue(userId, out var connectedParticipant);    // get from disconnected participants

            if (disconnectedParticipant != null && connectedParticipant == null)
            {
                // if participant exists in disconnected dictionary. Move it to Connected
                disconnectedParticipant.Participant.Status = ChatParticipantStatus.Online;

                DisconnectedParticipants.TryRemove(userId, out var removedParticipant);
                AllConnectedParticipants.TryAdd(userId, removedParticipant);

                await Clients.All.SendAsync("friendsListChanged");
            }

            // if participant doesn't exist in disconnected dict. Fetch user and move it to Connected dictionary
            if (disconnectedParticipant == null && connectedParticipant == null)
            {
                var user = await _mediator.Send(new GetUserRequest(Guid.Parse(userId)));

                var newParticipant = _mapper.Map <ParticipantResponseViewModel>(user);

                newParticipant.Participant.Status = ChatParticipantStatus.Online;

                AllConnectedParticipants.TryAdd(userId, newParticipant);
            }
            // else if connected participant exists.. do nothing because user probably logged in on multiple browsers
        }
示例#2
0
        /// <summary>
        /// Disconnected handler
        /// Switches user to different pool for disconnected users and updates his status to offline
        /// </summary>
        /// <param name="exception"></param>
        /// <returns></returns>
        public override async Task OnDisconnectedAsync(Exception exception)
        {
            var userId = Context.User.Identity.Name;

            AllConnectedParticipants.TryGetValue(userId, out var participant);

            if (participant != null)
            {
                participant.Participant.Status = ChatParticipantStatus.Offline;

                AllConnectedParticipants.TryRemove(userId, out var removedParticipant);
                DisconnectedParticipants.TryAdd(userId, removedParticipant);

                await Clients.All.SendAsync("friendsListChanged");
            }
        }