投票工作多长时间 [英] How long polling works

查看:112
本文介绍了投票工作多长时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在研究ajax长轮询,但我很困惑。传统的ajax调用和长轮询有什么不同

I am studying the ajax long polling but I am confused. what is different in traditional ajax calls and long polling

   var lpOnComplete = function(response) {
   alert(response);
   // do more processing
   lpStart();
  };

    var lpStart = function() {
    $.post('/path/to/script', {}, lpOnComplete, 'json');
    };

    $(document).ready(lpStart);

这个例子只是以递归的方式调用服务器..有什么不同于传统的调用setInterval ..

this example is just calling in recursive manner to the server.. what is different than the traditional call in setInterval..

推荐答案

顾名思义长轮询意味着长时间轮询某事。

As the name suggest Long Polling means polling something for a long time.

$.post('/path/to/script', {}, lpOnComplete, 'json');

以下是实际流程启动的内容,您对服务器上的某些脚本进行ajax调用,例如,它的 / path / to / script ,你需要让你的服务器脚本(例如 php )足够聪明它只在需要的数据可用时响应请求,脚本应该等待指定的时间段(例如1分钟),如果没有数据可用,最多1分钟,那么它应该返回没有数据。

Here is what the actual process starts, You make an ajax call to some script on server, in this case its /path/to/script , You need to make your server script(php for example) smart enough so that it only respond to request's when required data is available, the script should wait for a specified time period(for example 1 minute) and if no data available upto 1 minute then it should return without data.

一旦服务器返回一些东西,在你的回调函数中你再次对同一个脚本进行ajax调用,服务器脚本再次继续这个过程。

As soon as server return something, in your callback function you again make an ajax call to the same script and the server script again continues the process.

考虑一个聊天应用程序,按照常规方式,即使没有消息可用,你也会每隔2秒钟轮询一次服务器并返回服务器。如果一分钟服务器没有给你新消息,你最终会在服务器上点击30次在最后一分钟。

Consider a chat application, In conventional way you are polling the server say every 2 second's and the server return's even if no messages are available.If upto one minute server get's no new messages for you, you end up hitting the server 30 times in last one minute.

现在考虑 长轮询 方式,您将服务器脚本设置为等待新消息的一分钟。从客户端,您对脚本进行一次ajax调用,并说明下一分钟没有消息到达,服务器将在1分钟后才响应。你在最后1分钟内只打了一次服务器。你能想象 30 Hit Vs 1 Hit

这篇关于投票工作多长时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆