我听说过反向ajax或彗星,并想过在我的聊天系统中实现它。
以前我必须这样做。
AJAX
updatePulls();
function updatePulls(){
$.post('resource/php/pull.php',{
latest_id : latest
}
,function(data){
...
}
setTimeout(updatePulls,5000); //5 seconds
},'json');
}
PHP
...
$user_id = $_SESSION['user_id'];
$data = mysqli_query($con,"SELECT * FROM messages WHERE message_id > $latest_id AND ... ");
...
/*
while($row = mysqli_fetch_assoc($data)){} and other stuffs
and finally return at every call from client
*/
echo json_encode($data);
现在,在我听说有关实施它的想法之后,有人能告诉我该怎么办?它说关于制作一个无限循环,然后我来做这个,但我对此非常肯定。
COMET PHP
...
$user_id = $_SESSION['user_id'];
$data = mysqli_query($con,"SELECT * FROM messages WHERE message_id > $latest_id AND ... ");
...
while($anythingnew < 1 ){
//again mysqli_query and increment $anythingnew accordingly
}
/*
while($row = mysqli_fetch_assoc($data)) and other stuffs
and finally return at every call from client
*/
echo json_encode($data);
请帮助。