多客户端同步

发布于 2025-01-01 09:00:47 字数 234 浏览 0 评论 0原文

我面临的问题是:我有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

远昼 2025-01-08 09:00:47

您可以使用以下两种方法来检查延迟。

那么就可以计算出用户的请求到达时间

请求实际时间 = 请求到达时间 - 延迟

请求到达时间必须是服务器时间(请求到达服务器时)所以我们不需要同步单位。但延迟数据必须与请求数据一起来自客户端。您必须创建一个客户端脚本来轮询和计算平均延迟时间。


第一种方法是取自stackoverflow中的一个问题。这个使用ajax。这是我搜索过的最准确的,与实际偏差不到 10 毫秒。它的作用是调用(通过 ajax)您服务器的页面(示例中的“/”url 是您的 Web 根目录)

  • 优点:我们使用 jQuery ajax .success()在收到回复后但在加载回复数据之前触发一个事件(因此请求大小并不重要)

  • 缺点:ajax 不能跨域(无需帮助)。但如果你有自己的服务器,没问题。


第二个是取自这里,我对其进行了一些修改。这最初是作为服务器测试器创建的,用于测试服务器是否仍然存在。

  • 优点:跨域(我们使用img = new Image()“图像预加载”方法)

  • 缺点:网速。有效负载大小(在本例中为图像)和互联网速度很重要,因为我们仅使用在内容加载后触发的 .onLoad()

根据图像尺寸的不同,这一步会有大约 200-400ms 的偏差。

//this is a static class. values are preserved. do resets before and after use.
var ping = {

    //the sample image. make it as small as possible like 1 x 1 px black and white. 
    //we are only testing ping, not download times
    //replace with your own image on your server since this link will die soon
    picture: "http://205.196.122.17/vh8cvmdtgfsg/8nsd22kphe1fz5w/spacer.bmp",

    //placeholder for test subject
    pictureFrame: null,

    //timer
    timer: null,

    reset: function(){
        //clear timeouts and timer
        clearTimeout(ping.timer);
        ping.timer = null;

        //clear the picture frame
        ping.pictureFrame = null;
    },

    //start ping function
    init: function() {

        //reset
        ping.reset();

        //get time before request
        var preSess = new Date();
        var preTime = preSess.getTime();

        //append current timestamp so request won't be from cache
        var pictureUri = ping.picture + "?time=" + preTime; 

        //create placeholder    
        ping.pictureFrame = new Image();
        ping.pictureFrame.onload = function() {

            //get time after load
            var postSess = new Date();
        var postTime = postSess.getTime();
            var requestTime = postTime - preTime;

            alert("Ping took "+requestTime+"ms");

            //reset
        ping.reset();
        };

        //triggers loading
        ping.pictureFrame.src = pictureUri;

        //set maximum timeout (in ms) before we declare domain not there
        ping.timer = setTimeout("ping.failed()", 60000);
    },

    //time-out reached
    failed: function() {
        //reset
        ping.reset();

        //alert what happened
        alert("Ping took too long");
    }
};

ping.init();

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.

//this is a static class. values are preserved. do resets before and after use.
var ping = {

    //the sample image. make it as small as possible like 1 x 1 px black and white. 
    //we are only testing ping, not download times
    //replace with your own image on your server since this link will die soon
    picture: "http://205.196.122.17/vh8cvmdtgfsg/8nsd22kphe1fz5w/spacer.bmp",

    //placeholder for test subject
    pictureFrame: null,

    //timer
    timer: null,

    reset: function(){
        //clear timeouts and timer
        clearTimeout(ping.timer);
        ping.timer = null;

        //clear the picture frame
        ping.pictureFrame = null;
    },

    //start ping function
    init: function() {

        //reset
        ping.reset();

        //get time before request
        var preSess = new Date();
        var preTime = preSess.getTime();

        //append current timestamp so request won't be from cache
        var pictureUri = ping.picture + "?time=" + preTime; 

        //create placeholder    
        ping.pictureFrame = new Image();
        ping.pictureFrame.onload = function() {

            //get time after load
            var postSess = new Date();
        var postTime = postSess.getTime();
            var requestTime = postTime - preTime;

            alert("Ping took "+requestTime+"ms");

            //reset
        ping.reset();
        };

        //triggers loading
        ping.pictureFrame.src = pictureUri;

        //set maximum timeout (in ms) before we declare domain not there
        ping.timer = setTimeout("ping.failed()", 60000);
    },

    //time-out reached
    failed: function() {
        //reset
        ping.reset();

        //alert what happened
        alert("Ping took too long");
    }
};

ping.init();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文