コメットテクニックを使ったチャットシステムを実装しました。ここにリンクがあります(ajaxを使用した2番目の方法を参照)comet with ajax
私は2つのブラウザと2つのアカウントで使用しています.1つのアカウントでメッセージを送信すると、もう1つの受信者はそれを独自の名前で受信します. コードは次のとおりです。
handleResponse: function(response)
{
$('chat_id_box').innerHTML += '<u class="myId">' + response['name'] + '</u>: <p class="talk">' + response['msg'] + '</p></br>';
},
こちらがコントローラー
$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 world!と入力するとします。 次に、2 番目のブラウザーで、このメッセージをabc: helo world!として受け取ります。 abcとxyzは 2 人のユーザーです。コードの問題は何ですか?理解できません。ありがとう..