1

I wanted to implement Comet in PHP and came across this page:

http://www.zeitoun.net/articles/comet_and_php/start

The second method explained in the article works fine for me. In the backend php file, the loop seems to be infinite:

// infinite loop until the data file is not modified
  $lastmodif    = isset($_GET['timestamp']) ? $_GET['timestamp'] : 0;
  $currentmodif = filemtime($filename);
  while ($currentmodif <= $lastmodif) // check if the data file has been modified
  {
    usleep(10000); // sleep 10ms to unload the CPU
    clearstatcache();
    $currentmodif = filemtime($filename);
  }

When the client leaves the page, how to tell the server to stop processing the loop? Otherwise I fear that the loop is going to go on and on on the server until something is modified.

Nirmal
  • 9,391
  • 11
  • 57
  • 81

1 Answers1

1

You need to check the connection_status function.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • The PHP documentations says: `The default behaviour is however for your script to be aborted when the remote client disconnects.` Does this mean that when the client that initiated the AJAX request disconnects, the loop will be break? – Nirmal Dec 25 '09 at 04:49
  • The best thing to do is test it and find out. – JAL Dec 25 '09 at 05:43
  • Thank you. I shall play with this and find out. Will update when done fixing it. – Nirmal Dec 25 '09 at 10:06