最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - PHP Comet. How to do it better? - Stack Overflow

programmeradmin0浏览0评论

I have a simple et chat. JavaScript send ajax request with long polling. When server find new messages in the database, it answers and gives JSON. Next, JavaScript send the request again.

Javascript:

function etConnect(){
$.ajax({
      cache:false,
      type:"get",
      data:'ts='+ts,
      url: urlBack,
      async: true,
      success: function (arr1) {
      //work with JSON
      //.....
      },
      plete:function(){
        etConnect(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 monMessage 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?

I have a simple et chat. JavaScript send ajax request with long polling. When server find new messages in the database, it answers and gives JSON. Next, JavaScript send the request again.

Javascript:

function etConnect(){
$.ajax({
      cache:false,
      type:"get",
      data:'ts='+ts,
      url: urlBack,
      async: true,
      success: function (arr1) {
      //work with JSON
      //.....
      },
      plete:function(){
        etConnect(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 monMessage 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?

Share Improve this question edited Feb 18, 2011 at 8:34 Reigel Gallarde 65.3k21 gold badges125 silver badges142 bronze badges asked Feb 18, 2011 at 8:28 Sir HallySir Hally 2,3583 gold badges32 silver badges48 bronze badges 1
  • 3 Don't leave an SQL injection hole. $lastmodif = (int)$lastmodif; – Alexey Lebedev Commented Feb 18, 2011 at 8:54
Add a ment  | 

2 Answers 2

Reset to default 6

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

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)

  1. 暂无评论