views:

166

answers:

3

I'm starting to use GameKit for a p2p project, and I want to call a function at the same time on both clients. Assuming their clocks aren't set to exactly the same time, how can I call NSTimer or whatever so my function is called at the same time on both clients?

Thanks!

+2  A: 

If it's just between two devices connected via Bluetooth then you can assign one as the master clock and the other as slave. When first connected, master sends over its own current time. The slave can use this to calculate the delta between its own clock and the server. It would then offset any timers by the delta and use that value to set a timer (the master uses its own clock, the client its own clock +/- diff w/ master clock). That way, both sides are in relative sync with each other.

Another option is to get the time from a server (or NIST if you need absolute time values) then calculate the delta between the current iPhone time and the server time. Once you have that, you don't rely on the local time alone but always offset it by the delta.

This is reasonably accurate, but doesn't allow for network transmission delays. If you need to be down to the millisecond accurate, then you may have to get fancier and try to compute some sort of average network delay to help close the gap.

Ramin
A: 

The best way would be to have a server that queries both clients to get their clock times and then calculate the time difference and so finally send a custom time to each client.

yan bellavance
A: 

Really, the bigger question is: why do you need to do this? Frankly, it's a giant pain, and if it's "so both parties see the same thing happen simultaneously," then it's probably orders of magnitude easier just to have one party be the "server" and tell the other parties to initiate the action, which will differ by the network latency (probably a handful of milliseconds).

Tim Keating