AJAX轮询频率 - 长轮询或不长轮询? [英] AJAX Polling Frequency - To long poll or not to long poll?

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

问题描述

我建设,需要相对稳定的数据库查询网页的一个组成部分。我可以看到两种不同的方法对于这一点,我想知道,如果他们中的一个比别人更好,或者如果我错过了第三个选项。

I'm building a component of a web page that needs relatively constant database polling. I can see two different approaches for this, and I'm wondering if one of them is better than the others, or if I'm missing a third option.

1)发送过一个AJAX请求每1或2秒来检查更新。每个请求立即返回,无论是否有新的数据。
2)关火一个AJAX请求将不会返回,直到它接收数据或发生超时。当任一这些情况的发生,这触发了下一个请求。 (我想,这就是所谓的长轮询?)

1) Send off an AJAX request every 1 or 2 seconds to check for updates. Each request returns immediately whether or not there is new data.
2) Fire off a single AJAX request that will not return until it receives data or a timeout occurs. Upon either of those happening, it fires off the next request. (I think this is called long polling?)

数据库查询的数量将是相同的与任一,但与#2将有更少的请求从可节省带宽和客户资源浏览器击发。对于服务器,是它更好地有保持活跃和查询之间休眠,或将触发每隔几秒钟,民意调查数据库一个​​PHP请求,然后关闭?或者是有没有什么区别,我强调这个太过分了?

The number of database queries will be the same with either, but with #2 there would be fewer requests firing from the browser which could save bandwidth and client resources. For the server, is it better to have a single PHP request that stays active and sleeps between queries, or that fires up every few seconds, polls the DB, then shuts down? Or is there no difference and I'm stressing about this too much?

编辑:我想我也应该指出,这是一个更大的网络应用程序中的聊天工具。在通信的稍微延迟不会杀用户,聊天是一个次要特征。

I suppose I should also state that this is a chat widget of a larger web app. A slight delay in communication is not going to kill a user, as chat is a secondary feature.

推荐答案

长轮询将规模更好(即更少的服务器负载),比轮询,同时给予更好的响应时间。

Long polling will scale better (i.e. less server load) than polling, while giving much better response times.

如果您的收件人投票,消息的平均行程时间将一半的轮询间隔。

If your recipient polls, the average journey time of a message will be half your poll interval.

使用长轮询,它的瞬间 - 服务器只等待如果没有什么可说的。

With long polling, its instant - the server only waits if there is nothing to say.

如果你正在做的聊天消息,去长轮询;其可用性的事情。

If you are doing chat messaging, go long poll; its a usability thing.

的下侧长轮询是更复杂的实现;但它不是要复杂得多,并且它广泛实施。所以,如果你不能用一个现成的,现成的框架,你所选择的Web服务器,您可以着手写作时合理的,你会得到它的工作。

The down-side with long polling is it is more complicated to implement; but its not that much more complicated, and it is widely implemented. So if you can't use an off-the-shelf framework for your webserver of choice, you can set about writing one reasonably and you will get it working.

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

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