// // Summary: // Attempts to accept a new connection for a given chat room. Can fail if the room is full, for example. public async Task JoinChatRoom(string chatRoomName, string userName) { var userConnectionId = Context.ConnectionId; _logger.LogInformation("A new user {userName:l} want to join room {chatRoomName:l} with connection id: {userConnectionId:l}", userName, chatRoomName, userConnectionId); try { _logger.LogInformation("Fetching chat room connection pool to see if it's full..."); await _chatRoomService.AddUserToChatRoomConnectionPool(chatRoomName, userName, userConnectionId); } catch (Exception e) when (e is ObjectDoesNotExistException || e is ChatRoomIsFullException || e is UserIsAlreadyConnectedException) { throw new HubException(e.Message); // TODO: improve safety of the exception message with specific handler } _logger.LogInformation("Adding user to chat room group..."); await Groups.AddToGroupAsync(Context.ConnectionId, chatRoomName); _logger.LogInformation("Adding connection..."); await base.OnConnectedAsync(); _logger.LogInformation("Broadcasting new user message to the room..."); await BroadcastSystemMessageToChatRoom($"A new user has joined the room: {userName}", chatRoomName); }