Jump to content

Recommended Posts

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 ?

Link to comment
https://forums.phpfreaks.com/topic/223383-page-hangs/
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/223383-page-hangs/#findComment-1154732
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/223383-page-hangs/#findComment-1154743
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.