문제

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?

도움이 되었습니까?

해결책

[[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.

다른 팁

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top