Search Unity

Question [Relay] Client disconnects immediately after conencting

Discussion in 'Netcode for GameObjects' started by SenhorCastor, Jul 2, 2022.

  1. SenhorCastor

    SenhorCastor

    Joined:
    Mar 23, 2021
    Posts:
    6
    I am able to start a Host using relay using SetupRelay of the class at the end of this.

    I get an event NetworkManager.Singleton.OnClientConnectedCallback event on the headset that started the host. This gives me the 6 letter code,


    If I use JoinRelay with the code provided, I do get the client connected (
    NetworkManager.Singleton.StartClient() returns true)

    But immediately after I get a NetworkManager.Singleton.OnClientDisconnectCallback event on the headset of the client joining, which leads me to believe the client immediately gets disconnected from the server.

    On the headset that started the host, I do not get any event at all, neither NetworkManager.Singleton.OnClientConnectedCallback nor NetworkManager.Singleton.OnClientDisconnectCallback

    Any help appreciated





    Code (CSharp):
    1. public class RelayManager : Singleton<RelayManager>
    2. {
    3.     [SerializeField]
    4.     private string environment = "production";
    5.  
    6.     [SerializeField]
    7.     private int maxConnections = 5;
    8.  
    9.     public bool IsRelayEnabled => Transport() != null && Transport().Protocol == UnityTransport.ProtocolType.RelayUnityTransport;
    10.  
    11.     public UnityTransport Transport()
    12.     {
    13.         var unityTransport = NetworkManager.Singleton.gameObject.GetComponent<UnityTransport>();
    14.         return unityTransport;
    15.     }
    16.  
    17.     public void Start()
    18.     {
    19.         NetworkManager.Singleton.OnClientConnectedCallback += (id) =>
    20.         {
    21.             D.Log("CONNECTED");
    22.         };
    23.         NetworkManager.Singleton.OnClientDisconnectCallback += (id) =>
    24.         {
    25.             D.Log("DISCONNECTED");
    26.         };
    27.  
    28.     }
    29.  
    30.     public async Task<RelayHostData> SetupRelay()
    31.     {
    32.         D.Log("Relay Server Starting");
    33.         InitializationOptions options = new InitializationOptions().SetEnvironmentName(environment);
    34.         await UnityServices.InitializeAsync(options);
    35.  
    36.         if (!AuthenticationService.Instance.IsSignedIn)
    37.         {
    38.             await AuthenticationService.Instance.SignInAnonymouslyAsync();
    39.         }
    40.  
    41.         Allocation allocation = await Relay.Instance.CreateAllocationAsync(maxConnections);
    42.  
    43.         RelayHostData relayHostData = new RelayHostData
    44.         {
    45.             Key = allocation.Key,
    46.             Port = (ushort)allocation.RelayServer.Port,
    47.             AllocationID = allocation.AllocationId,
    48.             AllocationIDBytes = allocation.AllocationIdBytes,
    49.             IPv4Address = allocation.RelayServer.IpV4,
    50.             ConnectionData = allocation.ConnectionData
    51.         };
    52.  
    53.         relayHostData.JoinCode = await Relay.Instance.GetJoinCodeAsync(relayHostData.AllocationID);
    54.  
    55.         Transport().SetRelayServerData(
    56.                 relayHostData.IPv4Address,
    57.                 relayHostData.Port,
    58.                 relayHostData.AllocationIDBytes,
    59.                 relayHostData.Key,
    60.                 relayHostData.ConnectionData);
    61.  
    62.         D.Log("Relay Server generated join code: " + relayHostData.JoinCode);
    63.         return relayHostData;
    64.     }
    65.  
    66.     public async Task<RelayJoinData> JoinRelay(string joinCode)
    67.     {
    68.         D.Log("Relay Client Starting joining with code " + joinCode);
    69.         InitializationOptions options = new InitializationOptions().SetEnvironmentName(environment);
    70.         await UnityServices.InitializeAsync(options);
    71.         if (!AuthenticationService.Instance.IsSignedIn)
    72.         {
    73.             await AuthenticationService.Instance.SignInAnonymouslyAsync();
    74.         }
    75.  
    76.         try
    77.         {
    78.             JoinAllocation allocation = await Relay.Instance.JoinAllocationAsync(joinCode);
    79.             RelayJoinData relayJoinData = new RelayJoinData
    80.             {
    81.                 Key = allocation.Key,
    82.                 Port = (ushort)allocation.RelayServer.Port,
    83.                 AllocationID = allocation.AllocationId,
    84.                 AllocationIDBytes = allocation.AllocationIdBytes,
    85.                 ConnectionData = allocation.ConnectionData,
    86.                 HostConnectionData = allocation.HostConnectionData,
    87.                 IPv4Address = allocation.RelayServer.IpV4,
    88.                 JoinCode = joinCode
    89.             };
    90.             Transport().SetRelayServerData(
    91.                 relayJoinData.IPv4Address,
    92.                 relayJoinData.Port,
    93.                 relayJoinData.AllocationIDBytes,
    94.                 relayJoinData.Key,
    95.                 relayJoinData.ConnectionData,
    96.                 relayJoinData.HostConnectionData);
    97.             D.Log("Relay Client joined with join code: " + joinCode);
    98.             return relayJoinData;
    99.  
    100.         }
    101.         catch (Exception e)
    102.         {
    103.             throw new PreconditionException(PreconditionErrorCode.AUTH, e.Message);
    104.         }
    105.     }
    106. }
    107.  
     
  2. SenhorCastor

    SenhorCastor

    Joined:
    Mar 23, 2021
    Posts:
    6
    Found it,

    On NetworkManager, uncheck "Connection Approval"

    Or I guess implement the connection approval mechanism