简体   繁体   中英

How to I call a function at the same time on multiple iPhones?

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!

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.

最好的方法是让服务器查询两个客户端以获取其时钟时间,然后计算时间差,最后向每个客户端发送自定义时间。

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM