Failed to connect to server.

hello, i have this problem where when i make a connection with relay no network objects are spawned in the client side and after a minute or so this error comes out

Failed to connect to server.
UnityEngine.Debug:LogError (object)
Unity.Netcode.Transports.UTP.UnityTransport:ProcessEvent () (at ./Library/PackageCache/com.unity.netcode.gameobjects@1.2.0/Runtime/Transports/UTP/UnityTransport.cs:828)
Unity.Netcode.Transports.UTP.UnityTransport:Update () (at ./Library/PackageCache/com.unity.netcode.gameobjects@1.2.0/Runtime/Transports/UTP/UnityTransport.cs:877)

before adding the relay code netcode used to work perfectly.
i tried unsing both “udp” and “dtls” for the RelayServerData but it didn’t work, i tried “wss” as was mentioned in another thread but i got the error

ArgumentException: Invalid connection type: wss. Must be udp or dtls.
Unity.Networking.Transport.Relay.RelayServerData…ctor (Unity.Services.Relay.Models.Allocation allocation, System.String connectionType) (at ./Library/PackageCache/com.unity.transport@1.3.1/Runtime/Relay/RelayServerData.cs:88)
LobbyManager.CreateRelay () (at Assets/Scripts/Lobby/LobbyManager.cs:374)
LobbyManager.Start_Game () (at Assets/Scripts/Lobby/LobbyManager.cs:321)
System.Runtime.CompilerServices.AsyncMethodBuilderCore+<>c.b__7_0 (System.Object state) (at <83161767e3f34f4a9c1453ea3e22d1f1>:0)
UnityEngine.UnitySynchronizationContext+WorkRequest.Invoke () (at <0ae5d4e782f74095b2c8d38f4225786f>:0)
UnityEngine.UnitySynchronizationContext.Exec () (at <0ae5d4e782f74095b2c8d38f4225786f>:0)
UnityEngine.UnitySynchronizationContext.ExecuteTasks () (at <0ae5d4e782f74095b2c8d38f4225786f>:0)

the code for creating the relay

public async Task<string> CreateRelay()
    {
        Allocation allocation;
        string relayJoinCode;
        try
        {
            allocation = await RelayService.Instance.CreateAllocationAsync(3);
            Debug.Log(allocation);
        }
        catch (Exception e)
        {
            Debug.LogError($"Relay create allocation request failed {e.Message}");
            throw;
        }

        Debug.Log($"server: {allocation.ConnectionData[0]} {allocation.ConnectionData[1]}");
        Debug.Log($"server: {allocation.AllocationId}");

        try
        {
            relayJoinCode = await RelayService.Instance.GetJoinCodeAsync(allocation.AllocationId);
            Debug.Log("code"+relayJoinCode);
        }
        catch (Exception)
        {
            Debug.LogError("Relay create join code request failed");
            throw;
        }

        RelayServerData serverRelayUtilityTask =  new RelayServerData(allocation, "wss");
        NetworkManager.Singleton.GetComponent<UnityTransport>().SetRelayServerData(serverRelayUtilityTask);
        NetworkManager.Singleton.StartHost();

      
        return relayJoinCode;
    }

code for joining relay

public async void JoinRelay(string joinCode)
    {
        JoinAllocation allocation;
        try
        {
            allocation = await RelayService.Instance.JoinAllocationAsync(joinCode);
        }
        catch
        {
            Debug.LogError("Relay create join code request failed");
            throw;
        }

        Debug.Log($"client: {allocation.ConnectionData[0]} {allocation.ConnectionData[1]}");
        Debug.Log($"host: {allocation.HostConnectionData[0]} {allocation.HostConnectionData[1]}");
        Debug.Log($"client: {allocation.AllocationId}");

        RelayServerData clientRelayUtilityTask =  new RelayServerData(allocation, "wss");

      
        NetworkManager.Singleton.GetComponent<UnityTransport>().SetRelayServerData(clientRelayUtilityTask);

        NetworkManager.Singleton.StartClient(); 
    }

note: i tried using parallel sync and building the game for testing and both didn’t work

1 Like

Did you find a solution to this? Even I am facing this issue.

What version of Unity, Relay and Netcode.
And did you activate Relay on Unity Dashboard?

Same problem here.

Since the error is saying invalid connection type ‘wss’ you should try changing the connection type. In the code for joining, line 18, try replacing wss with ‘udp’ or ‘dtls’.

hi all, WebSocket is only supported in UTP 2+. Please check the UTP documentation here and here

I found out how to solve the problem by following the following steps:

  1. Update Netcode for GameObjects to v1.6
  2. Update Unity Transport to v2.02 (I had to go to the plus icon in the left-top of the Package Manager and click on “Add Package By Name” > com.unity.transport) (I found no other method to install the latest version because it did not appear in the package manager)
  3. In the Network Manager enable “Use Web Sockets” and in the code to connect to the Relay Server, use the next line of code:
    NetworkManager.Singleton.GetComponent<UnityTransport>().SetRelayServerData(new RelayServerData(allocation, "wss"));

I defined the “wss” instead “udp” or “dtls”