The server does the following when the client is connected:
int timestamp = NetworkTransport.GetNetworkTimestamp ();
string message = Time.time + "+" + timestamp;
byte error;
byte[] data = System.Text.Encoding.UTF8.GetBytes (message);
NetworkTransport.Send (info.hostID, info.connectionID, reliableChannel, data, data.Length, out error);
Debug.Log(timestamp);
The client then receives the message:
int recHostID;
int recConnectionID;
int recChannelID;
byte[] recBuffer = new byte[1024];
int bufferSize = 1024;
int dataSize;
byte error;
string data = "";
NetworkEventType recData = NetworkTransport.Receive (out recHostID, out recConnectionID, out recChannelID, recBuffer, bufferSize, out dataSize, out error);
if (recData == NetworkEventType.DataEvent){
data = System.Text.Encoding.UTF8.GetString (recBuffer).Trim ('\0');
string[] info = data.Split (new string[]{ "+" }, System.StringSplitOptions.None);
float serverTime = float.Parse (info [0]);
int timestamp = int.Parse (info [1]);
int delay = NetworkTransport.GetRemoteDelayTimeMS (recHostID, recConnectionID, timestamp, out error);
Debug.Log(timestamp +" "+delay+" "+(NetworkError)error);
}
The server will output something like 2245
The client output will be: 2245 0 WrongOperation
There are very few examples of this, so what am I doing wrong? The timestamp seems to be the same, shouldn’t it just work?
It’s unfortunate this has gone so long without an answer, because I had the same issue. I may have tracked down the issue, or at least I think this is what is happening.
“GetCurrentRTT will return rtt (not rtt/2) averaged by last 10 ping time periods. So if your ping timeout is 500 ms (default) it returns mean value of 10 pings or mean value in 5 sec…”
What appears to be happening in this case is that 10 ping time periods have not yet elapsed - so if you call this function before then, it will return the UNet equivalent of an InvalidOperationException (meaning that it is ‘not valid’ to call that function).
I was able to move past this issue by waiting a period of time before calling the function. In my network library that wraps LLAPI, there is a handshake process between the client and host, and I delayed that process by 1000ms and no longer received the WrongOperation error from this API. I think that waiting period could be shorter, I haven’t experimented with it yet, but maybe it just needs to be 10 * sendrate.
Hopefully Unity can update the documentation to be more clear in the future, and verify if this is indeed the reason WrongOperation might be returned. I think it is a common use case for client and server to ‘agree’ on network time at the beginning of a session, so that they don’t have to communicate about it as much later on.