【问题标题】:long polling doesn't stop长轮询不会停止
【发布时间】:2012-10-12 18:02:01
【问题描述】:

我尝试修改长轮询代码以适应我的情况。 我想统计mysql的总数,如果数字发生变化(增加或减少),它会通知我。

问题是while循环不断循环。我怎样才能阻止它?我只需要在总金额发生变化时收到通知。

<?php
include("db.php");
$result = mysql_query("SELECT sum( r ) as totalsum FROM (SELECT colA, colB, COUNT( * ) AS r FROM product GROUP BY productid ) AS t");
while($row = mysql_fetch_array($result))
{
    $old_totalsum = $row['totalsum']; 
}

?>
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
<script type="text/javascript" charset="utf-8">

var old_totalsum =<?php echo $old_totalsum; ?>;
function waitForMsg(){
$.ajax({
    type: "GET",
    url: "poll.php?old_totalsum=" + old_totalsum,
    async: true,
    cache: false,
        success: function(data){

        var json = "eval(+(" + data + ")+)";
    if(json['msg'] != "") {

        alert(data); 
    }

    old_msg_id = json['old_msg_id']; 
    setTimeout('waitForMsg()',1000);
    },
    error: function(XMLHttpRequest, textStatus, errorThrown){
    alert("error: " + textStatus + " (" + errorThrown + ")");
    setTimeout('waitForMsg()',15000);
    }


    });  // end ajax
} //end waitformsg


$(document).ready(function(){ 
 waitForMsg();
});

</script>
</head>
<body>
<div id="chat">
</div>
</body>
</html>
<----------------------------------------------------------->

poll.php
<----------------------------------------------------------->
<?php
include("db.php");

$old_totalsum = $_GET['old_totalsum']; 


$result = mysql_query("SELECT sum( r ) as totalsum FROM (SELECT colA, colB, COUNT( * ) AS r FROM product GROUP BY productid ) AS t");
while($row = mysql_fetch_array($result))
{
    $last_sum = $row['totalsum']; 
}



while($last_sum < $old_totalsum || $last_sum > $old_totalsum)


    while($last_sum <= $old_totalsum)
    {
    usleep(1000);
    clearstatcache();
    $result = mysql_query("SELECT sum( r ) as totalsum FROM (SELECT colA, colB, COUNT( * ) AS r FROM product GROUP BY productid ) AS t");
        while($row = mysql_fetch_array($result))
        {
         $last_sum = $row['totalsum'];
            //echo $last_sum;
            //return;
        }

    }





$response = array();
$response['msg'] = 'new';
$response['old_msg_id'] = $last_sum;
echo json_encode($response);
?>
<------------------------------------------->

【问题讨论】:

  • 哪个while 循环没有退出? while($last_sum &lt;= $old_totalsum)while($last_sum &lt; $old_totalsum || $last_sum &gt; $old_totalsum)
  • 哦抱歉没有 while($last_sum
  • while($last_sum

标签: php mysql long-integer polling


【解决方案1】:

我期待这样的逻辑:

$latest_sum= -1;
// Loop until sum from DB is different from what was passed  in
while( $latest_sum == -1 || $latest_sum!= $old_sum ){
  $result = mysql_query("SELECT sum( r ) as totalsum FROM (SELECT colA, colB, COUNT( * ) AS r FROM product GROUP BY productid ) AS t");
   while($row = mysql_fetch_array($result))
   {
      $lastest_sum = $row['totalsum']; 
   }
}

$response = array();
$response['msg'] = 'new';
$response['old_msg_id'] = $latest_sum;
echo json_encode($response);

如果更改需要很长时间,那么 Apache(或任何运行 PHP 的设备)可能会取消请求。这也会在昂贵的数据库上运行大量查询。

所以最好让你的循环在 javascript 中。它会调用一个函数,该函数只会从数据库中返回最新的总和。 javascript 应该在每个请求之间等待一秒或更长时间。

如果数据库的性能仍然太难,那么 php 应该以某种方式每分钟缓存一次值并返回缓存的值。

【讨论】:

  • 我可以先问一个问题,然后再测试您的方法。
  • 为什么 while($last_sum $old_totalsum) 不能退出?我假设 $last_sum 将被更新并导致此条件中断。
  • Dave 我应该把你的代码放在哪里?我看到你把 $latest_sum= -1;这将导致您的 while 循环始终为真,不是吗?那么这是否意味着它也会继续循环?
  • 哦..我只是再次测试,似乎条件还可以。但是当我在 index.php 中运行时,它现在不断向我显示一个弹出框,它来自 if(json['msg'] != "") { alert(data); }
  • 现在情况发生了逆转。如果我更改数据库以使数字更大或更小,它不会向我显示弹出窗口。如果我什么都不做并且数字相同,它会一直弹出!那么,我该如何扭转这种局面???
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-12
  • 1970-01-01
  • 1970-01-01
  • 2021-09-28
  • 1970-01-01
  • 2011-04-20
相关资源
最近更新 更多