Esempio n. 1
0
        public async Task Enter(string roomId, string email)
        {
            roomService.AddUserToRoom(roomId, userManager.FindByEmailAsync(email).GetAwaiter().GetResult().Id);
            await Groups.AddToGroupAsync(Context.ConnectionId, roomId);

            await Clients.Group(roomId).SendAsync("Notify", $"{email} вошел в чат");
        }
Esempio n. 2
0
        public async Task <object> JoinRoom(CreateUserInputModel userInput, string roomId)
        {
            User user;

            if (userInput.Id.HasValue)
            {
                if (_connectionService.CheckIfUserAlreadyConnected(roomId, userInput.Id.Value))
                {
                    return(new { success = false, message = "User already connected." });
                }
                user = _userService.GetUser(userInput.Id.Value);
            }
            else
            {
                // Create user and add to room
                user = _userService.CreateUser(userInput);
                _roomService.AddUserToRoom(roomId, user);
            }

            // Add connection
            var room = _roomService.GetRoom(roomId);

            _connectionService.AddConnection(user, room, Context.ConnectionId);
            var connections = _connectionService.GetRoomConnections(roomId);

            await Clients.Group(roomId).SendAsync("UserJoin", new { user });

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

            await Clients.Caller.SendAsync("AllowedIn", user);

            await Clients.Caller.SendAsync("UserList", connections.Select(c => c.User).ToList());

            await Clients.Caller.SendAsync("AllUsersList", _userService.GetUsersInRoom(roomId));

            await Clients.Caller.SendAsync("NewSpeaker", new { userId = room.CurrentSpeakerUserId, endTime = room.CurrentSpeakerEndTime, duration = room.CurrentSpeakerDuration });

            var chatHistory = _roomService.GetRoomChatHistory(room.Id, DateTime.UtcNow);

            return(new { success = true, chatHistory = chatHistory.Select(m => new { m.FromUserId, m.Text, m.CreatedDateTime }) });
        }