2012-01-05 18 views
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打开 - 是否会等待缓冲区完成? 谢谢。

+0

JavaScript需要被异步写入。 'checkNotify'是一个试图被覆盖而不是创建新实例的单个变量。 – rxgx 2012-01-05 08:20:12

回答

0

阿帕奇有一些功能,以支持COMET听说。你可以去那里。

或者另一种选择是在Glassfish Application Server中使用Grizzly框架。

或另一种解决方案是使用Lightstreamer。你猜怎么了?如果你编写自定义代码,它们可能在本地系统中正常工作,但它可能会在实时中给服务器带来太多麻烦。

所以我想更好的选择是找到一个已经建在很长一段时间

研究工作的东西。因为这更像是'黑客'而不是'技术'。