Question

In a server-client game using bluetooth connectivity (GameKit, specifically, for iOS6 compatibility), 1 or more client devices (iPhone/iTouch) send a timestamp to the server device (iPad) when a button on each client is tapped. i.e. each of the clients calls:

[[NSDate date] timeIntervalSince1970]

...and then sends to the server the value as an NSData payload.

When only 1 client is connected, the time being generated and sent to the server is the same as the time of the physical tap. When 2 or more clients are connected, the generated times differ significantly, even if all the clients are tapped at exactly the same time, e.g.

Client 1 and 2 simultaneously tapped:

  • client 1 timestamp = 1396974546.558433 (time interval since 1970)
  • client 2 timestamp = 1396974551.274747 (time interval since 1970)

i.e. client 2 is somehow 5 seconds out from client 1. While I'd accept a few milliseconds of difference since truly simultaneous tapping is hard, multiple seconds is unacceptable. Adding additional clients generates a more or less random difference in times, but usually several seconds.

To make things even more mysterious, the timestamps are being generated differently on the client: the timestamps being received at the server are the same as those being generated by the client (i.e. it's not something happening in transit or on receipt).

I'm at a loss for what's causing this. Any ideas?

Was it helpful?

Solution

[[NSDate date] timeIntervalSince1970] is relative to the time that's set on the device, so you can't rely on two devices sending the same time to your server.

I had a similar issue when developing something using Multipeer Connectivity, and ended up using a library called ios-ntp which uses time information from multiple NTP servers along with the latency of the request itself to derive the accurate time. It's worked pretty well for me.

OTHER TIPS

My guess would be the devices are, for whatever reason, off by 5 seconds apart

my reason for saying this is that the

[[NSDate date] timeIntervalSince1970]

usually has sub millisecond accuracy

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top