ajax长轮询 java web,正常的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:

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 ...?

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 polling

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:

Cr7KS.jpg

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

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 long polling

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:

wogF4.jpg

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.

The client side would look like this:

refresh = function() {

$('#myCurrentMoney').load('getCurrentMoney.php',function(){

refresh();

});

}

$(function(){

refresh();

});

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值