1
我创建一个通知系统,立即检查,看看是否有一个在线用户的任何新的通知。长轮询成为一个幽灵
这里是我的脚本至今:
//...include(myscripts.php)...
function notificationsCount($id)
{
if(is_numeric($id) && !empty($id) && $id>0) return mysql_result(mysql_query("SELECT COUNT(*) FROM notifications WHERE unRead='1' AND userID = '$id'"),0);
}
if($_GET['getNotificiationCount'] && $_GET[userID])
{
$current = notificationsCount($_SESSION['userID']);
$count = 0;
while($count<20 && !connection_aborted()){
$c = notificationsCount($_SESSION['userID']);
if($c!=$current && $c!=0)
exit($c);
$count++;
sleep(1);
}
exit("0");
}
客户端:
var checkNotify;
$(window).unload(function() { checkNotify.abort(); });
//Nav Notifications
function checkNotifications()
{
checkNotify = $.ajax({
url: "/notifications.php?getNotificiationCount=true&userID=<?=$_SESSION[userID]?>",
timeout: "30000",
cache: false,
success : function(data, textStatus, XMLHttpRequest) {
if(data!=0) {
$("#nav_notification_bubble").removeClass("displayoff");
$("#notification_count").html(data); }
checkNotifications();
},
error: function() {}
});
}
checkNotifications();
这工作,但是如果我离开这个页面时,Web服务器将不响应(在我的客户端) ,直到“notifications.php”脚本结束。
我能做些什么,如果客户离开页面杀死脚本?
我的Web服务器确实有Apache Gzip打开 - 是否会等待缓冲区完成? 谢谢。
JavaScript需要被异步写入。 'checkNotify'是一个试图被覆盖而不是创建新实例的单个变量。 – rxgx 2012-01-05 08:20:12