Search Unity

Failed to connect to server.

Discussion in 'Relay' started by Hala_aburajouh, Mar 27, 2023.

  1. Hala_aburajouh

    Hala_aburajouh

    Joined:
    Jan 27, 2023
    Posts:
    2
    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.<ThrowAsync>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
    Code (CSharp):
    1. public async Task<string> CreateRelay()
    2.     {
    3.         Allocation allocation;
    4.         string relayJoinCode;
    5.         try
    6.         {
    7.             allocation = await RelayService.Instance.CreateAllocationAsync(3);
    8.             Debug.Log(allocation);
    9.         }
    10.         catch (Exception e)
    11.         {
    12.             Debug.LogError($"Relay create allocation request failed {e.Message}");
    13.             throw;
    14.         }
    15.  
    16.         Debug.Log($"server: {allocation.ConnectionData[0]} {allocation.ConnectionData[1]}");
    17.         Debug.Log($"server: {allocation.AllocationId}");
    18.  
    19.         try
    20.         {
    21.             relayJoinCode = await RelayService.Instance.GetJoinCodeAsync(allocation.AllocationId);
    22.             Debug.Log("code"+relayJoinCode);
    23.         }
    24.         catch (Exception)
    25.         {
    26.             Debug.LogError("Relay create join code request failed");
    27.             throw;
    28.         }
    29.  
    30.         RelayServerData serverRelayUtilityTask =  new RelayServerData(allocation, "wss");
    31.         NetworkManager.Singleton.GetComponent<UnityTransport>().SetRelayServerData(serverRelayUtilityTask);
    32.         NetworkManager.Singleton.StartHost();
    33.  
    34.      
    35.         return relayJoinCode;
    36.     }
    code for joining relay
    Code (CSharp):
    1. public async void JoinRelay(string joinCode)
    2.     {
    3.         JoinAllocation allocation;
    4.         try
    5.         {
    6.             allocation = await RelayService.Instance.JoinAllocationAsync(joinCode);
    7.         }
    8.         catch
    9.         {
    10.             Debug.LogError("Relay create join code request failed");
    11.             throw;
    12.         }
    13.  
    14.         Debug.Log($"client: {allocation.ConnectionData[0]} {allocation.ConnectionData[1]}");
    15.         Debug.Log($"host: {allocation.HostConnectionData[0]} {allocation.HostConnectionData[1]}");
    16.         Debug.Log($"client: {allocation.AllocationId}");
    17.  
    18.         RelayServerData clientRelayUtilityTask =  new RelayServerData(allocation, "wss");
    19.  
    20.      
    21.         NetworkManager.Singleton.GetComponent<UnityTransport>().SetRelayServerData(clientRelayUtilityTask);
    22.  
    23.         NetworkManager.Singleton.StartClient();
    24.     }
    note: i tried using parallel sync and building the game for testing and both didn't work
     
    rahulchawla2801 likes this.
  2. rahulchawla2801

    rahulchawla2801

    Joined:
    Oct 5, 2021
    Posts:
    34
    Did you find a solution to this? Even I am facing this issue.
     
  3. Mj-Kkaya

    Mj-Kkaya

    Joined:
    Oct 10, 2017
    Posts:
    179
    What version of Unity, Relay and Netcode.
    And did you activate Relay on Unity Dashboard?
     
  4. UGameStudio

    UGameStudio

    Joined:
    May 4, 2019
    Posts:
    5
    Same problem here.
     
  5. zhare86

    zhare86

    Joined:
    Mar 28, 2017
    Posts:
    28
    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'.
     
  6. thiagownt

    thiagownt

    Unity Technologies

    Joined:
    Jul 30, 2021
    Posts:
    22
    hi all, WebSocket is only supported in UTP 2+. Please check the UTP documentation here and here
     
  7. UGameStudio

    UGameStudio

    Joined:
    May 4, 2019
    Posts:
    5
    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"