示例#1
0
        public async Task LeaveRoom(string group)
        {
            GetUserAndCurrentRoomChatView user = await _chatService.GetUserAndCurrentRoomByUserId(Context.User.Identity.Name);

            await Groups.RemoveFromGroupAsync(Context.ConnectionId, group);

            await Clients.Group(group).SendAsync("UserDisconnected", $"{user.FirstName} {user.LastName} has left converstion", user);
        }
示例#2
0
        public override async Task OnDisconnectedAsync(Exception ex)
        {
            GetUserAndCurrentRoomChatView user = await _chatService.GetUserAndCurrentRoomByUserId(Context.User.Identity.Name);

            GetAllRoomsChatView userRooms = await _chatService.GetByUserId(Context.User.Identity.Name);

            List <string> roomNames = userRooms.Rooms
                                      .Select(x => x.Name)
                                      .ToList();

            await Clients.Groups(roomNames).SendAsync("UserDisconnected", $"{user.FirstName} {user.LastName} has left conversation", user);

            await base.OnDisconnectedAsync(ex);
        }
        public async Task <GetUserAndCurrentRoomChatView> GetUserAndCurrentRoomByUserId(string userId)
        {
            User user = await _userManager.FindByIdAsync(userId);

            if (user == null)
            {
                throw new Exception("User is not find");
            }

            GetUserAndCurrentRoomChatView response = new GetUserAndCurrentRoomChatView();

            response = _mapper.Map(user, response);

            return(response);
        }
示例#4
0
        public async Task JoinRoom(string roomId, string group)
        {
            await _chatService.SetUserCurrentRoomByUserId(Context.User.Identity.Name, Guid.Parse(roomId));

            GetUserAndCurrentRoomChatView user = await _chatService.GetUserAndCurrentRoomByUserId(Context.User.Identity.Name);

            GetAllMessagesChatView room = await _chatService.GetAllMessagesByRoomId(Guid.Parse(roomId));

            GetAllUsersChatView usersInRoom = _chatService.GetAllUsersByRoomId(user.CurrentRoomId);

            await Groups.AddToGroupAsync(Context.ConnectionId, group);

            await Clients.Group(group).SendAsync("UserConnected", $"{user.FirstName} {user.LastName} join to conversation", user, usersInRoom);

            await Clients.Group(group).SendAsync("ReceiveRoomMessages", room);
        }
示例#5
0
        public override async Task OnConnectedAsync()
        {
            GetUserAndCurrentRoomChatView user = await _chatService.GetUserAndCurrentRoomByUserId(Context.User.Identity.Name);

            GetAllRoomsChatView userRooms = await _chatService.GetByUserId(Context.User.Identity.Name);

            List <string> roomNames = userRooms.Rooms
                                      .Where(x => x.Id == user.CurrentRoomId)
                                      .Select(x => x.Name)
                                      .ToList();

            GetAllUsersChatView usersInRoom = _chatService.GetAllUsersByRoomId(user.CurrentRoomId);
            await Clients.Groups(roomNames).SendAsync("UserConnected", $"{user.FirstName} {user.LastName} join to conversation", user, usersInRoom);

            await base.OnConnectedAsync();
        }