Search Unity

Resolved Is there any way to get connection endpoint from lobby players without using relay?

Discussion in 'Lobby' started by Ozgekocaoglu, May 24, 2022.

  1. Ozgekocaoglu

    Ozgekocaoglu

    Joined:
    Feb 13, 2019
    Posts:
    38
    Hello,
    I'm using lobby services and i'm trying to use also UDP, so i need lobby players connection points. But Lobby players and also Authentication services not gives me that info i think.


    Do you have any idea about that?
     
  2. veleek_unity

    veleek_unity

    Ben Randall Unity Technologies

    Joined:
    Aug 25, 2021
    Posts:
    59
    What do you mean by "connection points"? Are you referring to the IP address?
     
  3. veleek_unity

    veleek_unity

    Ben Randall Unity Technologies

    Joined:
    Aug 25, 2021
    Posts:
    59
    If you want to get the IP there are a few different ways to do so, but they're very dependent on your scenario.

    If you just want to enable users on the same WiFi network to connect to each other, you can (usually) just use network APIs to get the local IP. See this forum post for a few examples: https://answers.unity.com/questions/1731994/get-the-device-ip-address-from-unity.html

    If you want to enable users to connect over the internet, you usually need the external IP address. Often cases this is not enough though because depending on how they're connected to the internet there may be a bunch of additional configuration necessary to make sure that you can even connect to them using their external IP. There's some discussion about that here: https://forum.unity.com/threads/get-public-ip-address.9745/
     
  4. Ozgekocaoglu

    Ozgekocaoglu

    Joined:
    Feb 13, 2019
    Posts:
    38
    Thank you so much for you response.

    Yes i mean IP address.

    In my case, After connecting or creating a new lobby, i need their IPs. Because I'm also creating a data transportation layer with (com.unity.transport).

    Code (CSharp):
    1.  IEnumerator startNetworkAsClient()
    2.    {
    3.  
    4.     PlayerDriver = NetworkDriver.Create();
    5.     System.Net.IPAddress serverAddress = System.Net.IPAddress.Parse(endPointRemote);
    6.  
    7.     Unity.Collections.NativeArray<byte> nativeArrayAddress;
    8.     nativeArrayAddress = new  Unity.Collections.NativeArray<byte>(serverAddress.GetAddressBytes().Length, Unity.Collections.Allocator.Temp);  
    9.     nativeArrayAddress.CopyFrom(serverAddress.GetAddressBytes());
    10.  
    11.     NetworkEndPoint endpoint = NetworkEndPoint.LoopbackIpv4;
    12.     endpoint.SetRawAddressBytes(nativeArrayAddress);
    13.     endpoint.Port = portNumber;
    14.     clientConnection = PlayerDriver.Connect(endpoint);
    15.     Debug.Log("Connected: " + clientConnection.IsCreated);
    16.     }
    Code (CSharp):
    1.   IEnumerator startNetworkAsHost()
    2.   {
    3.     HostDriver = NetworkDriver.Create();
    4.     if (HostDriver.Bind(NetworkEndPoint.AnyIpv4) != 0)
    5.     {
    6.       Debug.LogError("Server failed to bind");
    7.     }
    8.     else
    9.     {
    10.       while (!HostDriver.Bound)
    11.       {
    12.         HostDriver.ScheduleUpdate().Complete();
    13.         yield return null;
    14.       }
    15.  
    16.       if (HostDriver.Listen() != 0)
    17.       {
    18.         Debug.LogError("Server failed to listen");
    19.       }
    20.       else
    21.       {
    22.         Debug.Log("Server connected");
    23.       }
    24.     }
    25.  
    26.  
    27.   }

    I've tried to keep host endpoint with Lobby options data.

    Code (CSharp):
    1.  
    2.  var lobbyData = new Dictionary<string, DataObject>()
    3.     {
    4.         ["Endpoint Address"] = new DataObject(DataObject.VisibilityOptions.Public, GetPublicIP()),
    5.     };
    6.     options.Data = lobbyData;
    7.     currentLobby = await Lobbies.Instance.CreateLobbyAsync(lobbyName: defaultLobbyName, maxPlayers: maxPlayers, options: options);
    8.  

    I edited my question. I can get external IP as you can see. Problem is after i connect with server, i can not send data or update server...

    Am i connecting with the right way?

    Update code block is:

    Code (CSharp):
    1.  if(currentLobby.HostId == loggedPlayer.Id){
    2.         HostDriver.ScheduleUpdate().Complete();
    3.  
    4.         NetworkConnection c;
    5.         while ((c = HostDriver.Accept()) != default(NetworkConnection))
    6.         {
    7.             hostConnections.Add(c);
    8.             Debug.Log("Accepted a connection");
    9.         }
    10.  
    11.      
    12.     DataStreamReader stream;
    13.     for (int i = 0; i < hostConnections.Length; i++)
    14.     {
    15.         NetworkEvent.Type cmd;
    16.         while ((cmd = HostDriver.PopEventForConnection(hostConnections[i], out stream)) != NetworkEvent.Type.Empty)
    17.         {
    18.  
    19.             if(cmd == NetworkEvent.Type.Connect){
    20.                 Debug.Log("Server connected");
    21.             }
    22.             if (cmd == NetworkEvent.Type.Data)
    23.             {
    24.  
    25.             }
    26.             else if (cmd == NetworkEvent.Type.Disconnect)
    27.             {
    28.                 Debug.Log("Client disconnected from server");
    29.                 hostConnections[i] = default(NetworkConnection);
    30.             }
    31.         }
    32.     }
    33.     }
    34.     else{
    35.             PlayerDriver.ScheduleUpdate().Complete();
    36.     }
     
    Last edited: May 25, 2022
  5. veleek_unity

    veleek_unity

    Ben Randall Unity Technologies

    Joined:
    Aug 25, 2021
    Posts:
    59
    Sorry, I'm not super familiar with the actual networking components (I'm mainly involved with the Lobby service). I'm going to see if I can find someone else who can help respond to this.

    When you say "you cannot send data or update server", are you getting an error or are you sending data and it just doesn't seem to arrive?
     
  6. Ozgekocaoglu

    Ozgekocaoglu

    Joined:
    Feb 13, 2019
    Posts:
    38
    No problem, I've asked kind a same question at (unity.transport) forums.

    https://forum.unity.com/threads/do-...-connection-with-com-unity-transport.1286642/

    I guess i understand where is the problem. Without relay, sending data between two machine that not involved same network needs own their configurations. So if i wanna use lobby services with unity trasportation, i should also use relay services.

    No, i'm not getting an error but yes, data doesn't seem to arrive. It seems I should find another way to send data between my clients.

    Thx for your responses and helps!
     
  7. mancGames

    mancGames

    Joined:
    Mar 25, 2021
    Posts:
    3
    I know this is an old post but I have the same problem here. I implemented the Lobby service perfectly. Also, I have dedicated server and I don't want to use Unity Relay servers. In relay documentation it says it's possible but how?
     
  8. veleek_unity

    veleek_unity

    Ben Randall Unity Technologies

    Joined:
    Aug 25, 2021
    Posts:
    59
    If you have a dedicated server, then all the players will be connecting to the server, not to each other. So I'm not sure that you need some way to get each lobby players connection information.

    I assume that one of the clients creates the dedicated server. That client should create the Lobby and include the dedicated server connection information in the Lobby. Then when others join the lobby they can use that information to connect to the server.

    Am I understanding your question correctly?
     
  9. amirloard

    amirloard

    Joined:
    Oct 23, 2020
    Posts:
    3
    hi i think he means this :
    i have dedicated server and wanna use lobby system to create lobby and connect player together without relay service . because lobby system only give you relay endpoint ! it is my question to, how to use lobby system to create lobby in our dedicated server without using relay ? very simple question if it is possible then just send us some document and if not just say no or ask from other devs that conterbuted in both relay and lobby and dedicated servers . thanks cheers
     
  10. RunninglVlan

    RunninglVlan

    Joined:
    Nov 6, 2018
    Posts:
    182
    As I understood from what veleek_unity said, we need to add server data to Lobby. AFAIK this is possible using e.g.
    Code (CSharp):
    1. await LobbyService.Instance.UpdateLobbyAsync(lobby.Id, new UpdateLobbyOptions {
    2.     Data = new Dictionary<string, DataObject> {
    3.         { KEY_IP, new DataObject(DataObject.VisibilityOptions.Member, serverIp) },
    4.         { KEY_PORT, new DataObject(DataObject.VisibilityOptions.Member, serverPort) }
    5.     }
    6. });
    Then clients can get this data:
    Code (CSharp):
    1. var unityTransport = NetworkManager.Singleton.GetComponent<UnityTransport>();
    2. unityTransport.SetConnectionData(lobby.Data[KEY_IP], ushort.Parse(lobby.Data[KEY_PORT]));