Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Is there a way of knowing how long the Disconnect timeout has been running for?

Discussion in 'Multiplayer' started by Zephilinox, Jul 9, 2015.

  1. Zephilinox

    Zephilinox

    Joined:
    Sep 15, 2014
    Posts:
    14
    In the NetworkManager I've set the Disconnect Timeout to 10 seconds and I would like to display an icon next to the player when they have been timed out for 2 seconds so that it's clear to every peer/host that the player has (temporarily or permanently) lost connection.

    So is there any way of getting this? I found http://docs.unity3d.com/ScriptReference/Networking.NetworkConnection-lastMessageTime.html which seems to be what I'm looking for, and http://docs.unity3d.com/ScriptReference/Networking.NetworkServer-connections.html can give me the list of all client connections, but where do I get the NetworkServer from? I can't find it in the NetworkManager Documentation.
     
  2. seanr

    seanr

    Unity Technologies

    Joined:
    Sep 22, 2014
    Posts:
    669
    it is static, so:

    Code (CSharp):
    1. for (var conn in NetworkServer.connections)
    2. {
    3.     if (conn != null)
    4.     {
    5.         // do stuff for connection
    6.     }
    7. }
     
  3. Zephilinox

    Zephilinox

    Joined:
    Sep 15, 2014
    Posts:
    14
    Hey, thanks for the reply. I did exactly that however lastMessageTime just seems to be ever-increasing. I have a little project set up with player movement which utilises a SyncVar and a Command. Running this function as host in the Update() of a script deriving from NetworkBehaviour. Moving the host player or the client player seems to have no effect on resetting this timer, is it based on something else?

    Code (CSharp):
    1.  
    2.   void test()
    3.   {
    4.       List<NetworkConnection> clients = NetworkServer.connections;
    5.       Debug.Log(clients.Count);
    6.       foreach (NetworkConnection netCon in clients)
    7.       {
    8.           if (netCon != null)
    9.           {
    10.               Debug.Log(netCon.lastMessageTime + "secs");
    11.           }
    12.       }
    13.   }
    14.  
    Output with just a host:


    Output with host and 1 client:

     
  4. seanr

    seanr

    Unity Technologies

    Joined:
    Sep 22, 2014
    Posts:
    669
    lastMessageTime is never "reset", it is the timestamp of the last time a message was recieved. You can compare it to Time.time to calculate how much time has passed.
     
    Zephilinox likes this.
  5. Zephilinox

    Zephilinox

    Joined:
    Sep 15, 2014
    Posts:
    14
    Ah I see, thank you, I didn't realise it was a Timestamp, I thought it was the time since the last message.

    This is my updated code and it works great.

    Code (c#):
    1.   void test()
    2.   {
    3.       for (int i = 0; i < NetworkServer.connections.Count; ++i)
    4.       {
    5.           NetworkConnection netCon = NetworkServer.connections[i];
    6.           if (netCon != null)
    7.           {
    8.               Debug.Log("Message Received from " + netCon.connectionId + " " + (Time.time - netCon.lastMessageTime) + " seconds ago.");
    9.           }
    10.       }
    11.   }
     
    Kristonitas likes this.