示例#1
0
        //
        // Summary:
        //   Connects a user to a given chat room by updating the chat room's active connection pool with the user's
        //   new connection. If the the user or chat room are not found or if the room is full, it raises an exception.
        //
        // Returns:
        //   ChatRoomConnectionPool - an updated chat room connection pool with the users new connection.
        //
        // Raises:
        //   ChatRoomIsFullException - the chat is full (risen by the chat room domain logic)
        //   ObjectDoesNotExistException - the chat room or the user were not found
        //   UserIsAlreadyConnectedException - the user is attempting to connect to a chat room in which he's already connected
        public async Task <ChatRoomConnectionPool> AddUserToChatRoomConnectionPool(string chatRoomName, string userName, string connectionId)
        {
            var chatRoomConnectionPool = await GetChatRoomConnectionPool(chatRoomName);

            var user = await _userService.GetUser(userName);

            var chatRoomId       = chatRoomConnectionPool.ChatRoomId;
            var serverInstanceId = GlobalState.ServerInstanceId;

            // domain logic to increment the chat room distributed connections
            var updatedChatRoomConnectionPool = ChatRoomLogic.IncrementChatRoomConnectionPool(user.Id, serverInstanceId, connectionId, chatRoomConnectionPool);
            await _redis.SetKey(chatRoomId, updatedChatRoomConnectionPool, null);

            return(updatedChatRoomConnectionPool);
        }
示例#2
0
        public async static Task <Tuple <ChatRoom, User, ChatRoomConnectionPool> > BuildScenarioWithChatRoomAndUserWithPreviousConnectionPool(string chatRoomName,
                                                                                                                                              int activeConnectionsLimit, string userName, DBContext db, RedisContext redis)
        {
            var chatRoomConnectionPool = BuildChatRoomConnectionPoolWithFourUsers();
            var newChatRoom            = new ChatRoom
            {
                Id   = chatRoomConnectionPool.ChatRoomId,
                Name = chatRoomName,
                ActiveConnectionsLimit = activeConnectionsLimit
            };

            var newUser = new User
            {
                Name = userName
            };

            // creates chat room with the same Id of the pool and a user
            await db.ChatRooms.AddAsync(newChatRoom);

            await db.Users.AddAsync(newUser);

            await db.SaveChangesAsync();

            // creates a connection pool for the chat room
            await redis.SetKey(chatRoomConnectionPool.ChatRoomId, chatRoomConnectionPool, null);

            return(new Tuple <ChatRoom, User, ChatRoomConnectionPool>(newChatRoom, newUser, chatRoomConnectionPool));
        }