javascript - PHP Comet. How to do it better? -
i have simple comet chat. javascript send ajax request long polling. when server find new messages in database, answers , gives json. next, javascript send request again.
javascript:
function cometconnect(){ $.ajax({ cache:false, type:"get", data:'ts='+ts, url: urlback, async: true, success: function (arr1) { //work 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 commonmessage modified>$lastmodif"); while($r=mysql_fetch_row($q)){ $flag=false; //prepare json... variable $resp //......... } usleep(5000); } echo $resp;
the problem following: "while($flag)" can execute long time (if nobody posts messages). so, apache can throw exeptions (max execution time, 502 bad gateway or gateway timeout).
how solve it?
use .htaccess , "php_value max_execution_time 0"?
or simple send new request javascript, when server returns error (it makes getting messages more slow)?
may be, there other way?
if there no messages push server in 40 sec, send response server, on basic of client re-request.
Comments
Post a Comment