gpt4 book ai didi

php - 长轮询变成幽灵

转载 作者:行者123 更新时间:2023-11-28 10:03:26 24 4
gpt4 key购买 nike

我正在创建一个通知系统,立即检查是否有针对在线用户的新通知。

这是到目前为止我的脚本:

//...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();

这是有效的,但是如果我离开页面,网络服务器将不会响应(在我的客户端),直到“notifications.php”脚本完成。

如果客户端离开页面,我该怎么做才能终止脚本?

我的 Web 服务器确实打开了 Apache Gzip - 会等待缓冲区完成吗?

最佳答案

我听说 Apache 有一些支持 COMET 的功能。你可以这么做。

或者另一种选择是在 Glassfish 应用服务器中使用 Grizzly 框架。

或者另一个解决方案是使用 Lightstreamer。你猜怎么了?如果您编写自定义代码,

它们在本地系统中可能工作正常,但实时时可能会给服务器带来太大麻烦。

所以我想更好的选择是找到已经构建了很长时间的东西

研究工作。因为这更像是“黑客”而不是“技术”。

关于php - 长轮询变成幽灵,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8738896/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com