简体   繁体   English

使用 GKSession 进行对等时如何获得精确的时钟同步?

[英]How to get precise clock synchronisation when doing peer to peer with GKSession?

I made a simple game which connects to other peers using GKSession from GameKit.我制作了一个简单的游戏,它使用 GameKit 中的 GKSession 连接到其他同行。 It was easy to set up but I discovered some problems:设置很容易,但我发现了一些问题:

  • Latency varies a lot.延迟变化很大。 Sometimes message arrives instantly on other devices.有时消息会立即到达其他设备。 Sometimes latency is > 1 second for sending data to other peers.有时,将数据发送到其他对等方的延迟 > 1 秒。 Data is only a string with 10 chars.数据只是一个包含 10 个字符的字符串。

  • My game depends on precise clock synchronisation.我的游戏依赖于精确的时钟同步。 The game has music playing and it sounds odd when it doesn't start playing the same time on all connected devices.游戏有音乐播放,当它没有在所有连接的设备上同时开始播放时听起来很奇怪。

I couldn't find anything in the documentation about how to snychroize timing.我在文档中找不到关于如何同步时间的任何内容。 The problem is that the "master" pier which starts the game immediately begins playing the music and then all other piers receive the message a little bit later and thus start playing after delay.问题是开始游戏的“主”码头立即开始播放音乐,然后所有其他码头稍晚收到消息,因此延迟后开始播放。

Then I tried to delay playing the game music after sending the start message to all piers, but sometimes latency is lower or higher and I just can't get a good reliable sync.然后我尝试在向所有码头发送开始消息后延迟播放游戏音乐,但有时延迟更低或更高,我无法获得良好的可靠同步。

Is there a open source framework which makes peer to peer and clock sync more reliable and easier to use on iOS?是否有开源框架可以让点对点和时钟同步在 iOS 上更可靠、更易于使用?

Unfortunately, you're in for a world of hurt here.不幸的是,你在这里受到了伤害。 It is very difficult to do precise clock synchronization over a network interface.通过网络接口进行精确的时钟同步非常困难。 You would need your devices to sync to within 20ms here.您需要您的设备在 20 毫秒内同步到这里。

I would recommend doing the following: send a bunch of ping packets at 50ms intervals, make the client respond immediately.我建议执行以下操作:以 50 毫秒的间隔发送一堆 ping 数据包,让客户端立即响应。 Take the average of this to give you average round trip time (RTT) and then halve that to estimate the one-way latency.取其平均值以获得平均往返时间 (RTT),然后将其减半以估计单向延迟。 Then send your 'start clock' message and spin in a loop until you've waited for your one-way latency and begin playing the music on your end.然后发送您的“开始时钟”消息并循环旋转,直到您等待单向延迟并开始播放音乐。 The client should play its music immediately upon receiving 'start clock'.客户端应在收到“开始时钟”后立即播放音乐。

There is a more robust long-term sync solution outlined here: http://en.wikipedia.org/wiki/Precision_Time_Protocol这里概述了一个更强大的长期同步解决方案: http : //en.wikipedia.org/wiki/Precision_Time_Protocol

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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