Get Latency and Ping players

Hi guys, I’m having a little bit of a struggle moving over to UNET from the legacy networking solution here :slight_smile:

In the legacy Unity networking I was able to tell the latency in seconds of each RPC call using the NetworkMessageInfo argument that was passed to the method but it seems that this is gone in UNET. What is the equivalent to this when using ClientRPC and Command? I need this information for compensation purposes.

Also how can I ping other players ? In the NetworkConnection class there are GetStatsIn and GetStatsOut but they don’t hold information about pings. There is also lastMessageTime but this is still not quite what I need. What is the substitute for Network.GetAveragePing in UNET ?

On NetworkClient there might be some functionality which would help you (though I’ve not tried these myself).

http://docs.unity3d.com/ScriptReference/Networking.NetworkClient.html

NetworkClient.GetRTT() and GetConnectionStats() are worth having a look into.

1 Like

Hi Dark, did you ever resolve the issue with the message timestamp/latency ? I need to know the time between sending and receiving a NetworkMessage. In the old networking system we had Network.time and messages had NetworkMessageInfo which stored information about when the message was sent. Based on that we could calculate the trip time. But in UNET there is no synchronized network time and therefore we can’t even make this functionality by ourselves.

Unfortunately I did not find any way other than just establishing an additional legacy network connection and getting Network.time from there. Which is an awful solution and I encourage you to not do that. I hope they add synchronized time and message timestamps in the future as my game depends on that.

Hope I’m not wasting your time, but maybe this could help a bit?

1 Like

Thanks for the link Enrico! I’ve seen all of these videos but unfortunately they didn’t help. I need to know the exact time a message took to travel from sending to receiving. This would include the traveling time and the time spent in the buffer.
The legacy networking system took care of this pretty easily but just having a synchronized time between machines. Then upon receiving you can just compare the send timestamp of the message with the current time. Unfortunately I can’t seem to find a synchronized time variable in UNET.

I’ve also found this in the release notes for the latest version
Networking: Timing service is working now
Don’t really know what this is but maybe it means something

Have you also looked in the NetworkConnection class? There are things like lastMessageTime and some statistics things.

Hmm, timing service doesn’t ring a bell here either.
lastMessageTime is supposed to return the last time a message has been received on the connection.
I found this: http://docs.unity3d.com/ScriptReference/Networking.NetworkTransport.GetNetworkTimestamp.html and I think it might be what I’m after but since I’m using a network manager and I’m not initializing the NetworkTransport, I don’t know if it’s going to work. And also I don’t know if this is synchronized between all the peers.

NetworkTransport.GetCurrentRtt seems accurate on the server in 5.1.1f1 Windows.

How do I use GetCurrentRTT? How do I find the host ID and my connectionID to use as parameters for the function?

for (int i = 0; i < NetworkServer.connections.Count; ++i) {
    NetworkConnection c = NetworkServer.connections[i];
   if (c == null) {
      continue;
   }
   byte error;
   int rtt = NetworkTransport.GetCurrentRtt(c.hostId, c.connectionId, out error);
}
1 Like

I tested NetworkTransport.timestamp and it doesn’t seem to be synchronized between peers. That’s really bad. It says in the docs that it could be used to find message latency but I don’t think that’s possible after all. The only method I can come up with so far for having a synchronized timer would be ping-pong messages but there’s gotta be a better way!

Was this changed? NetworkTransport.GetCurrentRtt returns 0 for me on 5.3.

It works for us on our servers. Are you testing on a server or a client?

Testing on a client in 5.3.2 - NetworkTransport.GetCurrenRtt returns 0 for me as well.

Might be obvious to some, but GetCurrenRtt has been deprecated for GetCurrenRTT
https://docs.unity3d.com/ScriptReference/Networking.NetworkTransport.GetCurrentRTT.html

public class MyClass: NetworkBehaviour {
    private NetworkClient client;
    void Start() {
        client = GameObject.Find("NetworkManager").GetComponent<NetworkManager>().client;
    }
    void Update() {
        Debug.Log(client.GetRTT());
    }
}

Hello, i was wondering if this is still usable?