Calculate latency

Post here your questions about the Objective-C API for SFS2X

Moderators: Bax, Lapo, A51Integrated

Calculate latency

Postby janheuninck » 02 Apr 2012, 17:48

Hi,

I'm trying to display the latency on my client, but I can't get it to work properly. I searched on this forum and I found you guys recommend to use the lagmonitor in combination with the PingPong event. However, I can't find this in the iOS API.

Right now I calculate the latency by adding a unix timestamp to my packages sent from the server with:
Code: Select all
System.currentTimeMillis()


On client side I read this timestamp, and compare it with the current time on the client:
Code: Select all
        // Extract the timestamp from the message
        double timestamp = [[dictionary objectForKey:TIMESTAMP_KEY] doubleValue];

        // Calculate the latency
        NSNumber *systemTimestamp = [NSNumber numberWithDouble:1000.0 * [[NSDate date] timeIntervalSince1970]];
        int latency = (int) ([systemTimestamp doubleValue] - timestamp);


This should work, since I'm using the unix timestamps to calculate the latency. The result of this calculation is always +- 2750ms, which isn't correct (local network + network communication feels very responsive). Am I doing something wrong? Or are there better ways to do this?

Thanks
User avatar
janheuninck
 
Posts: 68
Joined: 23 Nov 2011, 01:35
Location: Vancouver BC, Canada

Re: Calculate latency

Postby A51Integrated » 02 Apr 2012, 18:46

Have the timestamp originate on the client - send it as an extension request or an object request. Simply turn it around on the server side and pass it back to the client. Then compare the current time with the received timestamp and you will have your round trip latency.

In short, the server's time may be slightly out of sync with your client. Better to originate timestamps on the same device.
A51 Integrated
http://a51integrated.com / +1 416-703-2300
User avatar
A51Integrated
 
Posts: 240
Joined: 03 Jan 2012, 19:55
Location: Toronto, Canada

Re: Calculate latency

Postby janheuninck » 02 Apr 2012, 20:02

I've implemented it the way you suggested. Thanks!
User avatar
janheuninck
 
Posts: 68
Joined: 23 Nov 2011, 01:35
Location: Vancouver BC, Canada


Return to SFS2X iPhone / iPad / OSX API

Who is online

Users browsing this forum: No registered users and 2 guests