2012-09-12 22 views
0

我已經在這裏發佈了一個類似的問題,但沒有得到一個答案,將解決我的問題,也問題已經改變了一點,所以我重新張貼並絕望得到一些幫助!ajax長輪詢在頁面刷新崩潰鏈接到另一頁

鏈接到前面的問題:

ajax long polling with mysql

當前代碼:

JS(我從PHP中運行它):

$oldIDq = mysql_query("SELECT * FROM messages ORDER BY id DESC LIMIT 1"); 
while($oldrow = mysql_fetch_array($oldIDq)){ 
$oldID = $oldrow['id'];  
} 

$func = ' 
var oldID = '.$oldID.'; 

function wait() { 
$.ajax({ 
    type: "GET", 
    url: "../scripts/msg_scripts/msg.php?oldid=" + oldID, 
    async: true, 
    cache: false, 

    success: function (data){ 

     if(data != \'1\'){ 
      var json = eval(\'(\' + data + \')\'); 
      if (json[\'msg_content\'] != "") { 
        alert("new meassage added"); 
        } 
        oldID = json[\'oldID\']; 
        setTimeout(\'wait()\',1000); } 

    }, 

    disconnect: function() 
    { 
     return false; 
     setTimeout(\'wait()\',1000); 
    }, 

    error: function(XMLHttpRequest, textStatus, errorThrown){ 
     alert("error: " + textStatus + "(" + errorThrown + ")"); 
     setTimeout(\'wait()\',1000); 
    } 

}); 
} 

$(document).ready(function(){ 

    wait(); 
}); 
'; 

SERVER:

$connect = mysql_connect ("localhost", "root", "") 

or die ("couldnt connect"); 
mysql_select_db ("***") or die ("not found"); //if db was not found die 
mysql_query("SET NAMES 'utf8'"); 

$oldID = $_GET['oldid']; 

if($oldID == "") { 

die('timeout'); 
} 
else{ 

$result = mysql_query("SELECT id FROM messages ORDER BY id DESC LIMIT 1"); 
while($row = mysql_fetch_array($result)) 
{ 
    $last_msg_id = $row['id']; 
} 
while($last_msg_id <= $oldID) 
{ 
    usleep(10000); 
    clearstatcache(); 
    $result = mysql_query("SELECT id FROM messages ORDER BY id DESC LIMIT 1"); 
    while($row = mysql_fetch_array($result)) 
    { 
     $last_msg_id = $row['id']; 
    } 
} 



$response = array(); 
$response['msg_content'] = 'new'; 
$response['oldID'] = $last_msg_id; 
echo json_encode($response); 
} 

現在,我有一個會話在進程的服務器端運行,我現在刪除它,因爲我明白,長輪詢有會話的問題我也有運行在發送ajax請求的頁面上的會話,因爲我刪除了會議我的問題在某種程度上得到了改善,現在發生的情況是,我基本上可以點擊我網站上的一個鏈接並退出網頁併發生錯誤,但如果我做了4-5次以上,瀏覽器就會凍結每一個點擊任何鏈接只是重新運行ajax函數,我得到一個不同的錯誤。如果我刷新請求的頁面,我會無意中得到第二個錯誤,並且瀏覽器凍結。另外如果這是有用的信息,如果我關閉瀏覽器,並嘗試重新打開我的網站的任何頁面,它根本不會加載,除非我重新運行我的服務器(現在在本地主機上工作)也嘗試過使用chrome和ff。

有人可以指點我的解決方案嗎?

+0

它不會有一個問題,「會話」如果你只需用[session_write_close](http://php.net/manual/en/function.session-write-close.php)關閉寫入會話 – dbf

+0

我已經做了它,但它沒有影響 – durian

+0

抱歉地說,但那麼你做錯了 – dbf

回答

1

- 再次更新提取所有新郵件

閱讀你的代碼,你只希望返回的最後一條消息,如果是的話那麼while循環在這種情況下很沒用。千萬記住,有可能是OLDID並插入到你跳過你的數據庫中的最後一個ID之間更「新」的消息,因此,這段代碼我提供

$connect = mysql_connect ("localhost", "root", "") 
or die ("couldnt connect"); 
mysql_select_db ("***") or die ("not found"); //if db was not found die 
mysql_query("SET NAMES 'utf8'"); 

$oldID = trim($_GET['oldid']); 

// empty response, you may fill it with a default msg 
$response = array(
    'msg_content' => 'none', 
    'oldID' => $oldID 
); 

// this if statement will prevent to return a valid 
// JSON string to the ajax request 
if(empty($oldID)) { 
die('timeout'); 
} 
else { 
    $result = mysql_query("SELECT id FROM messages WHERE id > ".addslashes($oldID)." ORDER BY id DESC"); 
    $index = 1; 
    // check if results have new messages 
    if(($num_rows = mysql_num_rows($result) > 0) { 
    $response['msg_content'] = 'new'; 
    while($row = mysql_fetch_array($result)) { 
     $response['new_msgs'][] = $row['id'] 
     if($index == $num_rows) 
     $response['oldID'] = $row['id']; // set oldID to last record 
    } 
} 
echo json_encode($response); 

-

您的意見如何正確使用session_write_close

session_start(); 
    $var_id = $_SESSION['id']; 
    $var_fn = $_SESSION['firstname']; 
    $var_ln = $_SESSION['lastname']; 
    $var_mail = $_SESSION['email']; 
// close write to session here to release it for other sources 
session_write_close(); 

if (!loggedin()){ 
    header ("Location: index.php");} 
    if ($_GET['id']) { 
    $id = mysql_real_escape_string($_GET['id']);} 
    // you are using session here, use the localized $var_id 
    else if (isset($var_id)) { 
    $id = mysql_real_escape_string($var_id); 
} 

session_start()被調用時,該點的會話鎖定用於寫入任何其他來源,除了當前範圍(執行PHP文件),它在,這是爲了確保沒有值可以是在讀取會話值時更改。

documentation

會話數據通常存儲在將腳本終止,而無需調用session_write_close(),但作爲會話數據被鎖定,以防止併發只寫一個腳本,可以在一個會話操作任何時候。將幀集與會話一起使用時,由於此鎖定,您將體驗逐幀加載的幀。只要完成對會話變量的所有更改,就可以通過結束會話來縮短加載所有幀所需的時間。

卡住問題,我覺得while循環是無止境的,從您的瀏覽器http://example.com/pathto/scripts/msg_scripts/msg.php請求頁面,看看會發生什麼

$counter = 0; 
while($last_msg_id <= $oldID) 
{ 
    usleep(10); // changing to be a bit faster 
    clearstatcache(); 
    $result = mysql_query("SELECT id FROM messages ORDER BY id DESC LIMIT 1"); 
    $row = mysql_fetch_array($result); 
    $last_msg_id = $row['id']; 
    $counter++; 

    if($counter > 100) 
     break; 
} 
echo "counted: {$counter}"; 
exit(); 
+0

好吧,我做到了這一點,但仍然沒有結果.. – durian

+0

它計數101 .. – durian

+0

無限循環,嘗試更新回答的第一個代碼 – dbf