我已經實現了一個聊天系統使用彗星技術。這裏是鏈接(請參閱使用ajax的第二種方法)comet with ajaxCOMET與經典的AJAX聊天
當我用一個帳戶發送消息時,另一個接收方用自己的名稱接收它,我在2個瀏覽器和2個帳戶中使用它。 下面是代碼:
handleResponse: function(response)
{
$('chat_id_box').innerHTML += '<u class="myId">' + response['name'] + '</u>: <p class="talk">' + response['msg'] + '</p></br>';
},
這裏S控制器
$filename = dirname(__FILE__).'./data.txt';
$name = $this->session->userdata('name');
// store new message in the file
$msg = isset($_GET['msg']) ? $_GET['msg'] : '';
if ($msg != '')
{
file_put_contents($filename,$msg.$name);
die();
}
// infinite loop until the data file is not modified
$lastmodif = isset($_GET['timestamp']) ? $_GET['timestamp'] : 0;
$currentmodif = filemtime($filename);
while ($currentmodif <= $lastmodif) // check if the data file has been modified
{
usleep(10000); // sleep 10ms to unload the CPU
clearstatcache();
$currentmodif = filemtime($filename);
}
// return a json array
$response = array();
$response['msg'] = file_get_contents($filename);
$response['name'] = file_get_contents($filename);
$response['timestamp'] = $currentmodif;
echo json_encode($response);
flush();
想,如果我輸入XYZ:HELO世界!然後在第二個瀏覽器中,我收到此消息爲abc:helo world!abc和xyz有2個用戶。代碼中有什麼問題?我無法理解。 謝謝..
燦你發佈完整的代碼?我們需要充分理解您的代碼才能回答您的問題。 – Licson 2013-03-01 14:34:59
是的,我有郵政編碼 – 2013-03-01 14:44:54