0

我听说过 reverse-ajax 或 comets,并想在我的聊天系统中实现它。

以前我必须做这些事情。

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

现在,在我听到评论后,我想到了实施它,但谁能告诉我我该怎么做?它说要制作一个无限循环,然后我来做这个,但我不太确定。

彗星 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);

请提供任何帮助。

4

0 回答 0