English 中文(简体)
Ajax long polling (comet) + php on lighttpd v1.4.22 multiple instances problem
原标题:

I am new to this site, so I really hope I will provide all the necessary information regarding my question.

I ve been trying to create a "new message arrived notification" using long polling. Currently I am initiating the polling request by window.onLoad event of each page in my site.

On the server side I have an infinite loop:

while(1){
 if(NewMessageArrived($current_user))break;
 sleep(10);
}
echo $newMessageCount;

On the client side I have the following (simplified) ajax functions:

poll_new_messages(){
 xmlhttp=GetXmlHttpObject();
 //...
 xmlhttp.onreadystatechange=got_new_message_count;
 //...
 xmlhttp.send();
}

got_new_message_count(){
 if (xmlhttp.readyState==4){
  updateMessageCount(xmlhttp.responseText);
  //...
  poll_new_messages();
 }
}

The problem is that with each page load, the above loop starts again. The result is multiple infinite loops for each user that eventually make my server hang.

*The NewMessageArived() function queries MySQL DB for new unread messages.

*At the beginning of the php script I run start_session() in order to obtain the $current_user value.

I am currently the only user of this site so it is easy for me to debug this behavior by writing time() to a file inside this loop. What I see is that the file is being written more often than once in 10 seconds, but it starts only when I go from page to page.

Please let me know if any additional information might help.

Thank you.

问题回答

I think I found a solution to my problem. I would appreciate if anyone could tell, if this is the technique that is being used in COMET and how scalable this solution.

I used a user based semaphore like this:

$sem_id = sem_get($current_user);
sem_acquire($sem_id);
while(1){
 if(NewMessageArrived($current_user))break;
 sleep(10);
}
sem_release($sem_id);
echo $newMessageCount;

It seems common for long-polling requests to timeout after 30 seconds. So in your while loop you could echo CLOSE after 30 seconds.

while(!$new_message && $timer < 30){
    $new_message = NewMessageArrived($current_user);
    if(!$new_message) {
        sleep(10);
        $timer += 10;
    }
}
if($newMessageCount) {
    echo $newMessageCount;
} else {
    echo  CLOSE ;
}

In the Javascript, you can listen for the CLOSE.

function poll_new_messages(){
    xmlhttp=GetXmlHttpObject();
    //...
    xmlhttp.onreadystatechange=got_new_message_count;
    //...
    xmlhttp.send();
}

function got_new_message_count(){
    if (xmlhttp.readyState==4){
        if(xmlhttp.responseText !=  CLOSE ) {
            updateMessageCount(xmlhttp.responseText);
        }
        //...
        poll_new_messages();
    }
}

Now, the PHP will return a response within 30 seconds, no matter what. If you use stays on the page, and you receive a CLOSE, you just don t update the count on the page, and re-ask.

If the user moves to a new page, your PHP instance will stop the loop regardless within 30 seconds, and return a response. Being on a new page though, the XHR that cared about that connection no longer exists, so it won t start up another loop.

You might try checking connection_aborted() periodically. Note that connection_aborted() might not pick up on the fact that the connection has in fact been aborted until you ve written some output and done a flush().

In fact, just producing some output periodically may be sufficient for php to notice the connection close itself, and automatically kill your script.





相关问题
selected text in iframe

How to get a selected text inside a iframe. I my page i m having a iframe which is editable true. So how can i get the selected text in that iframe.

How to fire event handlers on the link using javascript

I would like to click a link in my page using javascript. I would like to Fire event handlers on the link without navigating. How can this be done? This has to work both in firefox and Internet ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

javascript debugging question

I have a large javascript which I didn t write but I need to use it and I m slowely going trough it trying to figure out what does it do and how, I m using alert to print out what it does but now I ...

Parsing date like twitter

I ve made a little forum and I want parse the date on newest posts like twitter, you know "posted 40 minutes ago ","posted 1 hour ago"... What s the best way ? Thanx.

热门标签