多客户端同步
我面临的问题是:我有 N 个 iPad 可以触发事件。如何在服务器端知道谁首先触发了事件。服务器端使用的语言是 PHP,客户端使用的语言是 JavaScript (JQuery)。最大的问题是延迟,所以简单地发送 AJAX 轮询是行不通的,因为我可以在 j 之前按下按钮,但由于延迟,服务器可以在 i 之前收到 j 请求。此外,节省印刷时间也不是最佳选择,因为 iPad 不同步到毫秒或更小的单位。也许有某种协议可以处理这个问题,我可以从中得到一些想法?
The problem im facing is: I have N IPads which can trigger an event. How to know on server side who did trigger the event first. Languages used for server side is PHP and for client side JavaScript (JQuery). The biggest problem is latency, so simply sending AJAX polling would not work, since i could press button before j but server could get j request before i due to latency. Also saving press time is no optimal since IPads are not synchronized to milliseconds or smaller units. Maybe there is some kind a protocol, which deals with this from which I could get some ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用以下两种方法来检查延迟。
那么就可以计算出用户的请求到达时间
请求实际时间 = 请求到达时间 - 延迟
请求到达时间必须是服务器时间(请求到达服务器时)所以我们不需要同步单位。但延迟数据必须与请求数据一起来自客户端。您必须创建一个客户端脚本来轮询和计算平均延迟时间。
第一种方法是取自stackoverflow中的一个问题。这个使用ajax。这是我搜索过的最准确的,与实际偏差不到 10 毫秒。它的作用是调用(通过 ajax)您服务器的页面(示例中的“/”url 是您的 Web 根目录)
优点:我们使用 jQuery ajax
.success()
在收到回复后但在加载回复数据之前触发一个事件(因此请求大小并不重要)缺点:ajax 不能跨域(无需帮助)。但如果你有自己的服务器,没问题。
第二个是取自这里,我对其进行了一些修改。这最初是作为服务器测试器创建的,用于测试服务器是否仍然存在。
优点:跨域(我们使用
img = new Image()
“图像预加载”方法)缺点:网速。有效负载大小(在本例中为图像)和互联网速度很重要,因为我们仅使用在内容加载后触发的
.onLoad()
。根据图像尺寸的不同,这一步会有大约 200-400ms 的偏差。
here are 2 methods you can use to check latency.
then you can calculate the user's request arrival time
actual time of request = request arrival time - latency
request arrival time must be server time (when the request arrives on the server) side so we don't need to sync units. but the latency data must come from client along with the request data. you must create a client-side script to poll and calculate average latency times.
the first method is taken from a question here in stackoverflow. this one uses ajax. this is the most accurate i have ever searched, with less than 10ms deviation from actual. what it does is it calls (via ajax) a page of your server (the "/" url in the example is your web root)
advantage: we use jQuery ajax
.success()
which fires an event after the reply is received but before loading the reply data (thus request size doesn't matter)disadvantage: ajax does not cross-domain (without aid). but if you have your own server, no problems.
the second one is taken from here and i modified it a bit. this originally was created as a server tester to test if the server is still there.
advantages: cross-domain (we use
img = new Image()
"image preloader" method)disadvantage: the internet speed. payload size (in this case, an image) and internet speed will matter since we only use
.onLoad()
which fires after the content has loaded.there will be a deviation of about 200-400ms on this one, depending on the image size.