Search Unity

Question Client can StartClient() succesfully but ConnectedClients shows only the host

Discussion in 'Netcode for GameObjects' started by a1creator, Feb 21, 2023.

  1. a1creator

    a1creator

    Joined:
    Jan 18, 2021
    Posts:
    24
    I have a lobby that connects you through Relay automatically when you join. I have logs with success messages and they always work. Somehow, though, the client isn't a part of NetworkManager.Singleton.ConnectedClients.
    I don't get any errors when joining or disconnecting, but obviously none of my netcode stuff works because the client is not part of the server even though I join (at least, I thought) correctly.

    This is my host/join code.
    Code (CSharp):
    1. public async Task<bool> CreateRelay()
    2.     {
    3.         if (relayIsActive) return false;
    4.         try
    5.         {
    6.             relayIsActive = true;
    7.  
    8.             // we would create with 'maxPlayers' but we cant change it after, in case the host wants more players in lobby.
    9.             // so we use the developer-preset maximum amount
    10.             allocation = await RelayService.Instance.CreateAllocationAsync(lobbyManager.PredeterminedMaxPlayerAmount);
    11.  
    12.             JoinCode = await RelayService.Instance.GetJoinCodeAsync(allocation.AllocationId);
    13.  
    14.             relayServerData = new RelayServerData(allocation, "dtls");
    15.  
    16.             var success = StartNetworking(true);
    17.  
    18.             return success;
    19.         }
    20.         catch (RelayServiceException e)
    21.         {
    22.             lobbyManager.DisplayError(e);
    23.         }
    24.         return false;
    25.     }
    26.  
    27.     private async void JoinRelay(string joinCode)
    28.     {
    29.         if (relayIsActive) return;
    30.         try
    31.         {
    32.             relayIsActive = true;
    33.  
    34.             var joinallocation = await RelayService.Instance.JoinAllocationAsync(joinCode);
    35.  
    36.             relayServerData = new RelayServerData(joinallocation, "dtls");
    37.  
    38.             StartNetworking(false);
    39.         }
    40.         catch (RelayServiceException e)
    41.         {
    42.             lobbyManager.DisplayError(e);
    43.         }
    44.     }
    45.  
    46.     /// <returns>returns true if succesful</returns>
    47.     public bool StartNetworking(bool asHost)
    48.     {
    49.         NetworkManager.Singleton.GetComponent<UnityTransport>().SetRelayServerData(relayServerData);
    50.  
    51.         bool success;
    52.  
    53.         if (asHost)
    54.             success = NetworkManager.Singleton.StartHost();
    55.         else
    56.             success = NetworkManager.Singleton.StartClient();
    57.  
    58.         a1_Logs.Log("Started network as" + (asHost ? " host" : " client") + (success ? " successfully" : " unsuccessfully"));
    59.  
    60.         return success;
    61.     }
    I call this when polling information, as a debug test, and it returns 1 no matter how many players are (supposed to be) connected through relay when called on the host.
    Code (CSharp):
    1. a1_Logs.LogWarning("players connected: " + NetworkManager.Singleton.ConnectedClients.Count);
     
  2. lavagoatGG

    lavagoatGG

    Joined:
    Apr 16, 2022
    Posts:
    229
    Are you sure the RelayServerData is correct? Is this the way it should be initiallized and is it not null?
     
    a1creator likes this.
  3. a1creator

    a1creator

    Joined:
    Jan 18, 2021
    Posts:
    24
    As you can see in the code snippet, the RelayServerData is set before calling StartClient(), so it should never be null and it should give an error if "await RelayService.Instance.JoinAllocationAsync" failed
     
  4. lavagoatGG

    lavagoatGG

    Joined:
    Apr 16, 2022
    Posts:
    229
    I am doing
    Code (CSharp):
    1. JoinAllocation allocation = await Relay.Instance.JoinAllocationAsync(joinCode);
    2.  
    3.             //Create Object
    4.             _joinData = new RelayJoinData
    5.             {
    6.                 Key = allocation.Key,
    7.                 Port = (ushort)allocation.RelayServer.Port,
    8.                 AllocationID = allocation.AllocationId,
    9.                 AllocationIDBytes = allocation.AllocationIdBytes,
    10.                 ConnectionData = allocation.ConnectionData,
    11.                 HostConnectionData = allocation.HostConnectionData,
    12.                 IPv4Adress = allocation.RelayServer.IpV4
    13.  
    14.             };
    15.  
    16.  
    17.             //set transports data
    18.             NetworkManager.Singleton.GetComponent<UnityTransport>().SetRelayServerData(
    19.                 _joinData.IPv4Adress,
    20.                 _joinData.Port,
    21.                 _joinData.AllocationIDBytes,
    22.                 _joinData.Key,
    23.                 _joinData.ConnectionData,
    24.                 _joinData.HostConnectionData);
    25.  
    26.             //Finally start the client
    27.             NetworkManager.Singleton.StartClient();
    This is what I use and it works. Maybe you have a problem outside your code
     
    a1creator likes this.
  5. a1creator

    a1creator

    Joined:
    Jan 18, 2021
    Posts:
    24
    Setting up the joinData makes no difference as you can pass the allocation instead, I'm pretty sure. It's just the old or new version. I don't think my allocation is the issue but I'll look into it