2012-10-12 112 views
0

我試圖修改長輪詢代碼以適應我的情況。 我想從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); 
?> 
<-------------------------------------------> 
+0

我沒有看到任何鏈接 – Dave

+0

請再次檢查 – user1042911

+0

哪個'while'循環不退出? 'while($ last_sum <= $ old_totalsum)'或'while($ last_sum <$ old_totalsum || $ last_sum> $ old_totalsum)' –

回答

0

我希望這樣的邏輯:

$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); 

如果需要很長的時間來改變,那麼阿帕奇(或任何正在運行PHP)將有可能取消該請求。這也將運行大量的數據庫查詢,這是昂貴的。

所以最好是在JavaScript中有你的循環。它會調用一個函數,它會從數據庫中返回最新的總和。 JavaScript應該在每個請求之間等待第二個或更多。

如果數據庫性能仍然太難,那麼PHP應該以某種方式每分鐘緩存一次該值並返回緩存的值。

+0

我可以在測試你的方法之前先問一個問題。 – user1042911

+0

爲什麼while($ last_sum <$ old_totalsum || $ last_sum> $ old_totalsum)無法退出?我假設$ last_sum將被更新並導致這個條件中斷。 – user1042911

+0

戴夫我應該把你的代碼放在哪裏?我看到你把$ latest_sum = -1;這會讓你的while循環總是成真的,不是嗎?那麼它意味着它會一直循環呢? – user1042911