开发者

PHP Comet. How to do it better?

I have a simple comet chat. JavaScript send ajax request with long p开发者_StackOverflowolling. When server find new messages in the database, it answers and gives JSON. Next, JavaScript send the request again.

Javascript:

function cometConnect(){
$.ajax({
      cache:false,
      type:"get",
      data:'ts='+ts,
      url: urlBack,
      async: true,
      success: function (arr1) {
      //work with JSON
      //.....
      },
      complete:function(){
        cometConnect(true);
        nerr=false;
      },
      dataType: "text"
    }); 
}

PHP

$flag=true;
$lastmodif = isset($_GET['ts']) ? $_GET['ts'] : 0;
while($flag){
  $q=mysql_query("SELECT text, posterId,modified, fromUserId,toUserId, login FROM commonMessage WHERE modified>$lastmodif");

      while($r=mysql_fetch_row($q)){
        $flag=false;
        //Prepare JSON... variable $resp
            //.........
      }

  usleep(5000); 
}
echo $resp;

the problem is following: this "while($flag)" can execute for a long time (if nobody posts messages). So, Apache can throw the exeptions (max execution time, sometimes 502 Bad Gateway or Gateway Timeout).

How to solve it?

use .htaccess and "php_value max_execution_time 0"?

or simple send new request from JavaScript, when server returns error (it makes getting messages more slow)?

May be, there is some other way?


You should check out APE Project. It's an Ajax Push Engine, it might help for realtime communication: www.ape-project.org


if there are no messages to push from server in say 40 sec, you send some response from server, on the basic of which the client re-request.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜