NetworkManager not keeping connection alive.

Hi! I’m currently implementing Relay connection on my project. Everything works fine until I try to connect a client to the relay, which gives me the erro “Relay code no found”. I followed the Using Relay with NGO documentation and I’m using Relay Unity Transport as the Protocol. I’ve read a few threads about this problem but none of them worked.
Here’s the Allocation code:

Allocation allocation = await Relay.Instance.CreateAllocationAsync(2);

        RelayData data = new RelayData
        {
            IPv4Address = allocation.ServerEndpoints.Find(item => item.ConnectionType == ("dtls")).Host,
            Port = (ushort)allocation.ServerEndpoints.Find(item => item.ConnectionType == ("dtls")).Port,
            AllocationID = allocation.AllocationId,
            AllocationIDBytes = allocation.AllocationIdBytes,
            ConnectionData = allocation.ConnectionData,
            Key = allocation.Key
        };

        string joinCode = await RelayBaseServices.GetJoinCode(data.AllocationID);

NetworkManager.Singleton.GetComponent<UnityTransport>().SetHostRelayData(
                data.IPv4Address,
                data.Port,
                data.AllocationIDBytes,
                data.Key,
                data.ConnectionData
                );

            NetworkManager.Singleton.StartHost();

And here’s the code to JoinAllocation:

JoinAllocation allocation = await Relay.Instance.JoinAllocationAsync(_currentlobby.Data["RelayJoinCode"].Value);

            RelayData data = new RelayData
            {
                IPv4Address = allocation.ServerEndpoints.Find(item => item.ConnectionType == ("dtls")).Host,
                Port = (ushort)allocation.ServerEndpoints.Find(item => item.ConnectionType == ("dtls")).Port,
                AllocationID = allocation.AllocationId,
                AllocationIDBytes = allocation.AllocationIdBytes,
                ConnectionData = allocation.ConnectionData,
                Key = allocation.Key,
                HostConnectionData = allocation.HostConnectionData
            };

NetworkManager.Singleton.GetComponent<UnityTransport>().SetClientRelayData(
                data.IPv4Address,
                data.Port,
                data.AllocationIDBytes,
                data.Key,
                data.ConnectionData,
                data.HostConnectionData
                );

            NetworkManager.Singleton.StartClient();

As you can see, I’m not calling JoinAllocation for the Host, as in this Thread ( Relay: Code Not Found ). And I immediatly call StartHost() and StartClient(), which should be enough to keep the connection alive, but it does not and 10 seconds later i get “relay code not found” if I try to connect (If I connect within 10 seconds the connection works fine). this Thread ( How to keep Relay connection alive using NGO? ) suggests a test using Interactive Relay Sample, I did the test and this error shows up even in the sample:

I would REALLY appreciate some help. Thank you!

I encountered the same issue before. For my case, I didn’t change anything in terms of code but just restarting the unity works.

I’m running into this too now – did you ever figure out the issue? Restarting Unity didn’t solve it for me either.

Hi @soh_zeta ,

Can you share Relay and Netcode version?
Maybe you could use “RelayServerData” data instead of “RelayData”.
And use “NetworkManager.Singleton.GetComponent().SetRelayServerData(RelayServerData);”

Have you ever read this documentation page?

Hey @soh_zeta ,

StartHost and StartClient should indeed be enough to maintain the connection open. Some ideas:

  • Which version of Netcode for Game Objects (NGO) are you using? If possible, try updating to the latest one.
  • On the Unity Transport, there is a Heartbeat Timeout MS configuration. What is its value currently? Make sure it isn’t zero