EchoFool Posted January 4, 2011 Share Posted January 4, 2011 Hey i have a script using COMET server push effect, but it causes my browser to hang and i cannot understand why. Heres the script that deals with PHP: <?php session_name('Current_User'); session_set_cookie_params(0, '/', '.mydomain.com'); // i use a subdomain so this is needed session_start(); include("connect.php"); function getNewMessagesSince($timestamp) { $Get = mysql_query("SELECT Count(push) AS Total FROM messages WHERE LastUpdate='$timestamp'") or die(mysql_error()); $row = mysql_fetch_assoc($Get); if($row['Total'] != 1) { $Get = mysql_query("UPDATE push SET LastUpdate='".time()."'") or die(mysql_error()); $Get = mysql_query("SELECT Message FROM messages WHERE Deleted='0' ORDER BY postedon DESC LIMIT 1") or die(mysql_error()); $row = mysql_fetch_assoc($Get); return array( 'lastupdate' => time(), 'html' => $row['Message'].' <br/>' ); } return false; } while(! ($row = getNewMessagesSince($_POST['lastupdate']))) { //every half a second usleep(50000); } echo json_encode($row); ?> Any ideas on what i got wrong ? Quote Link to comment https://forums.phpfreaks.com/topic/223383-page-hangs/ Share on other sites More sharing options...
BlueSkyIS Posted January 4, 2011 Share Posted January 4, 2011 infinite loop: while(! ($row = getNewMessagesSince($_POST['lastupdate']))) { //every half a second usleep(50000); } Quote Link to comment https://forums.phpfreaks.com/topic/223383-page-hangs/#findComment-1154721 Share on other sites More sharing options...
EchoFool Posted January 4, 2011 Author Share Posted January 4, 2011 Yeah i though that but i got this from a tutorial and theres doesn't do it =/ Which is why im confused Quote Link to comment https://forums.phpfreaks.com/topic/223383-page-hangs/#findComment-1154724 Share on other sites More sharing options...
BlueSkyIS Posted January 4, 2011 Share Posted January 4, 2011 $_POST['lastupdate'] never changes therefore $row = getNewMessagesSince($_POST['lastupdate']) is always false. therefore !($row = getNewMessagesSince($_POST['lastupdate'])) is always true. therefore you get an infinite loop. Quote Link to comment https://forums.phpfreaks.com/topic/223383-page-hangs/#findComment-1154726 Share on other sites More sharing options...
EchoFool Posted January 4, 2011 Author Share Posted January 4, 2011 It does change if a user posts a message. Plus the point of COMET JS is to use server push so the script keeps running http://www.zeitoun.net/articles/comet_and_php/start find with ctrl+ f : "The Backend Script" You will see they add an infinite loop on purpose cos its the only way to do Comet JS Quote Link to comment https://forums.phpfreaks.com/topic/223383-page-hangs/#findComment-1154732 Share on other sites More sharing options...
MadTechie Posted January 4, 2011 Share Posted January 4, 2011 You really need that flush(); for the output without it your need a lot of messages to be posted to fill up the apache output buffer Quote Link to comment https://forums.phpfreaks.com/topic/223383-page-hangs/#findComment-1154734 Share on other sites More sharing options...
EchoFool Posted January 4, 2011 Author Share Posted January 4, 2011 Do i put it in the while loop or after the json encode? Quote Link to comment https://forums.phpfreaks.com/topic/223383-page-hangs/#findComment-1154735 Share on other sites More sharing options...
MadTechie Posted January 4, 2011 Share Posted January 4, 2011 Well a COMET server rebuild the page (unless I am mistaken) the problem is apache is being sent nothing and waiting for output try this add to the start ini_set("output_buffering","Off"); ini_set("zlib.output_compression","Off"); //Loop while(! ($row = getNewMessagesSince($_POST['lastupdate']))) { //every half a second usleep(50000); echo ""; //Yes echo nothing.. flush(); // and output } Sounds strange but may just keep apache alive and in turn keep the browser pinging Quote Link to comment https://forums.phpfreaks.com/topic/223383-page-hangs/#findComment-1154743 Share on other sites More sharing options...
BlueSkyIS Posted January 4, 2011 Share Posted January 4, 2011 eh, my bad. i was looking at the script at the top of the page... need more coffee... Quote Link to comment https://forums.phpfreaks.com/topic/223383-page-hangs/#findComment-1154746 Share on other sites More sharing options...
EchoFool Posted January 4, 2011 Author Share Posted January 4, 2011 Thanks for the suggestion MadTechie, sadly it still hangs. And my entire session is for ever hanging on my domain so i have clear cookies to get it back to normal (which makes me wonder if its session related?) Quote Link to comment https://forums.phpfreaks.com/topic/223383-page-hangs/#findComment-1154748 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.