Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Unity Relay not working for 2 different machines connecting to eachother

Discussion in 'Multiplayer' started by TheDapperLad, Mar 22, 2023.

  1. TheDapperLad

    TheDapperLad

    Joined:
    Apr 10, 2022
    Posts:
    2
    Hello,

    When setting up Unity Netcode and Relay I was testing it on the same machine by running multiple instances, which is entirely functional. Where I started running into issues is when connecting two builds on two different computers. Instead of joining the same session they both became the hosts of two different lobbies. Even when trying to type in the join code manually they won't connect to each other.


    Here's my code:
    Code (CSharp):
    1. public class TestingNetcodeUI : MonoBehaviour
    2. {
    3.     string joinCode;  
    4.     private async void Awake()
    5.     {
    6.         await UnityServices.InitializeAsync();
    7.  
    8.         await AuthenticationService.Instance.SignInAnonymouslyAsync();
    9.         StartGame();
    10.     }
    11.  
    12.     private async void StartGame()
    13.     {
    14.         QueryResponse queryResponse = await UpdateLobbyList();
    15.         if (queryResponse.Results.Count >= 1) //if there is an active lobby, join it
    16.         {
    17.             Debug.Log("client");
    18.  
    19.             Debug.Log(joinCode + "join code");
    20.             Debug.Log(queryResponse.Results[0].Name + "lobby code");
    21.             JoinRelay(queryResponse.Results[0].Name); //gets the lobby it found and uses its name to join the same relay the host did
    22.         }
    23.         else //otherwise, make one
    24.         {
    25.             Debug.Log("host");
    26.             joinCode = await CreateRelay(); //creates relay and sets relay join code to this string
    27.             Lobby lobby = await LobbyService.Instance.CreateLobbyAsync(joinCode, 8); //makes relay join code the name of the lobby it just created
    28.             Debug.Log(joinCode + "join code");
    29.         }
    30.  
    31.     }
    32.  
    33.     private async Task<QueryResponse> UpdateLobbyList()
    34.     {
    35.         QueryResponse queryResponse = await Lobbies.Instance.QueryLobbiesAsync();
    36.  
    37.         return queryResponse;
    38.  
    39.     }
    40.  
    41.     private async Task<string> CreateRelay()
    42.     {
    43.         try
    44.         {
    45.             Allocation allocation = await RelayService.Instance.CreateAllocationAsync(10);
    46.  
    47.             string joinCode = await RelayService.Instance.GetJoinCodeAsync(allocation.AllocationId);
    48.  
    49.             RelayServerData relayServerData = new RelayServerData(allocation, "dtls");
    50.             NetworkManager.Singleton.GetComponent<UnityTransport>().SetRelayServerData(relayServerData);
    51.  
    52.             NetworkManager.Singleton.StartHost();
    53.  
    54.             return joinCode;
    55.         }
    56.         catch (RelayServiceException e)
    57.         {
    58.             Debug.Log(e);
    59.             return null;
    60.         }
    61.  
    62.     }
    63.  
    64.  
    65.     private async void JoinRelay(string joinCode)
    66.     {
    67.         try
    68.         {
    69.             Debug.Log("Joining Relay with" + joinCode);
    70.             JoinAllocation joinAllocation = await RelayService.Instance.JoinAllocationAsync(joinCode);
    71.  
    72.             RelayServerData relayServerData = new RelayServerData(joinAllocation, "dtls");
    73.             NetworkManager.Singleton.GetComponent<UnityTransport>().SetRelayServerData(relayServerData);
    74.  
    75.             NetworkManager.Singleton.StartClient();
    76.         }
    77.         catch (RelayServiceException e)
    78.         {
    79.             Debug.Log(e);
    80.             StartGame();
    81.         }
    82.  
    83.     }
    84. }
    Any help would be appreciated.
     
    Last edited: Mar 22, 2023
  2. TheDapperLad

    TheDapperLad

    Joined:
    Apr 10, 2022
    Posts:
    2
    Turns out that it was functional the entire time, there is just a silly feature that I didn't know about. Unity Lobbies have a "heartbeat", this heartbeat failing to return something in 30 seconds causes the lobby to go inactive and any lobby searches will filter it out. i fixed it by adding a coroutine that stimulates the lobby every 15 seconds so that it doesn't die.