长轮询...真的 [英] Long polling...really

查看:220
本文介绍了长轮询...真的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我学习了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调用服务器上的一些脚本,在这种情况下,它的 /路径/要/脚本,你需要做你的服务器脚本( 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秒数和服务器返回的即使没有消息available.If高达1分钟服务器获得的对你没有任何新的消息,则最终击中服务器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命中VS 1命中

这篇关于长轮询...真的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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