Search Unity

Transport: confirm connection is established

Discussion in 'Multiplayer' started by rogerdv, Nov 27, 2019.

  1. rogerdv

    rogerdv

    Joined:
    Jul 16, 2012
    Posts:
    90
    Im following the few samples there are about Transport, but so far I havent found a way to confirm if the connection was actually established, even if the server is down this function returns true:

    Code (CSharp):
    1. public bool Connect(string server)
    2.     {
    3.         driver = new UdpNetworkDriver(new ReliableUtility.Parameters { WindowSize = 32 });
    4.         pipeline = driver.CreatePipeline(typeof(ReliableSequencedPipelineStage));
    5.  
    6.         connection = new NativeArray<NetworkConnection>(1, Allocator.Persistent);
    7.         done = new NativeArray<byte>(1, Allocator.Persistent);
    8.  
    9.         var endpoint = new NetworkEndPoint();
    10.         endpoint = NetworkEndPoint.Parse(server, 9000);
    11.         connection[0] = driver.Connect(endpoint);
    12.         if (!connection[0].IsCreated)
    13.             return false;
    14.         else if (connection[0].GetState<UdpNetworkDriver>(driver) == NetworkConnection.State.Disconnected) {
    15.  
    16.             return false;
    17.         } else
    18.             return true;
    19.     }
    Can somebody see a problem in this code or maybe suggest improvements?
     
  2. Singtaa

    Singtaa

    Joined:
    Dec 14, 2010
    Posts:
    492
    NetworkConnection will always be in the "Connecting" state if server is not online. You can only confirm whether it's connected in an asynchronous manner (i.e. in subsequent jobs, after a set time interval).