普通ajax和长轮询之间的区别 [英] Difference between a normal ajax and long polling

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

问题描述

我试图了解有关长时间轮询以实时操纵"网站的更多信息,看到了一些视频,并且到目前为止,我还在思考:

I'm trying to understand more about long polling to "manipulate" a website in real time, saw some videos and I'm thinking so far:

说我有一个古老的日期,那就是sql,并且在它上面做一个回显.只要轮询就会知道旧日期是否与根据setInterval函数不时显示的日期不同...?

Say I have an old date that the sql and I make an echo on it. As long polling will know if the old date will not be the same as it will look from time to time according to the setInterval function ...?

说我想显示一个博客发布,其中所有文本都在mysql中,但是我发布了一个新的出版物,当时谁在页面上,您会看到发布时间(不是告诉我吗?) ,那么一个长时间的轮询代码将如何知道新旧出版物之间的区别?甚至不要给sql刻上相同或冲突的日期.

Say I want to show publication of a blog in which all text is in mysql, but repende I publish a new publication, and who is on the page at the time, you will see the publication time (not tell me?), Then how one long polling code will know the difference between the old and the new publication? Ate even not to give conflicting or repeating the same date engraved on the sql.

推荐答案

由于您最初的问题是两种技术之间的区别是什么,所以我将从这里开始:

Since your initial question was what the difference between the two techniques is, I will start with this:

AJAX轮询

使用AJAX轮询来更新页面意味着,您将在定义的时间间隔内向服务器发送请求,如下所示:

Using AJAX polling to update a page will mean, that you send a request in a defined interval to the server, which would look like this:

客户端向服务器发送请求,服务器立即响应.

The client sends a request to the server and the server responses immediately.

一个简单的示例(使用jQuery)如下所示:

A simple example (using jQuery) would look like this:

setInterval(function(){
    $('#myCurrentMoney').load('getCurrentMoney.php');
}, 30000);

问题是,这将导致很多无用的请求,因为在每个请求上不会总是有新事物.

The problem with this is, that this will cause a lot of useless requests since there won't be always new things on every request.

AJAX长轮询

使用AJAX长轮询将意味着,客户端向服务器发送请求,并且服务器在响应之前等待新数据可用.看起来像这样:

Using AJAX long polling will mean, that the client sends a request to the server and the server waits for new data to be available before he responds. This would look like this:

客户端发送请求,服务器不规则地"响应.服务器响应后,客户端将向服务器发送新请求.

The client sends a request and the server responds "irregularly". As soon as the server responds, the client will send a new request to the server.

客户端看起来像这样:

refresh = function() {
    $('#myCurrentMoney').load('getCurrentMoney.php',function(){
        refresh();
    });
}

$(function(){
    refresh();
});

这将只是将getCurrentMoney.php的输出加载到当前的money元素中,并且一旦有回调,就开始一个新请求.

What this will do is just load the getCurrentMoney.php's output into the current money element and as soon as there is a callback, start a new request.

在服务器端,通常使用循环.要解决您的服务器如何知道的问题,它们是新的发布:将最新的时间戳传递给客户端可用的发布到服务器,或者将长时间轮询开始"的时间用作索引:

On the server side you usually use a loop. To solve your question how the server will know, which are new publications: either you pass the timestamp of the newest to the client available publication to the server or you use the time of the "long polling start" as indiactor:

<?
$time = time();

while ($newestPost <= $time) {
    // note that this will not count as execution time on linux and you won't run into the 30 seconds timeout - if you wan't to be save you can use a for loop instead of the while
    sleep(10000);
    // getLatestPostTimestamp() should do a SELECT in your DB and get the timestamp of the latest post
    $newestPost = getLatestPostTimestamp();
}

// output whatever you wan't to give back to the client
echo "There are new posts available";

这里我们将不会有无用的"请求.

Here we won't have "useless" requests.

这篇关于普通ajax和长轮询之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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