0

您好,我正在使用标准的 comet co 和 iframe,但离开页面,该方法继续在服务器上运行。我需要检测连接何时关闭。循环必须是永久性的,而(真的),我不能定期完成。有人可以帮我谢谢

后端;

        set_time_limit(0);
        //Turn of Apache output compression
        // Necessary if you have gzip setup in your httpd.conf (e.g. LoadModule deflate_module modules/mod_deflate.so)
        apache_setenv('no-gzip', 1);
        ini_set('zlib.output_compression', 0);

        //Disable all PHP output buffering
        ini_set('output_buffering', 'Off');
        ini_set('implicit_flush', 1);
        ob_implicit_flush(1);

        for ($i = 0, $level = ob_get_level(); $i < $level; $i++) {
            ob_end_flush();
        } //Flush all levels of the buffer to start

        error_reporting(E_ALL);

        while( true ){
          $a = getAct();
          if($a)
          echo $a;
          $randSleep = mt_rand(400000, 600000);
          usleep($randSleep);
        }
4

1 回答 1

1

您可以使用 PHP 连接处理功能来做到这一点:

http://php.net/manual/en/features.connection-handling.php

// Ignore user aborts and allow the script
// to run forever
ignore_user_abort(true);
set_time_limit(0);

echo 'Testing connection handling in PHP';

// Run a pointless loop that sometime 
// hopefully will make us click away from 
// page or click the "Stop" button.
while(1)
{
    // Did the connection fail?
    if(connection_status() != CONNECTION_NORMAL)
    {
        break;
    }

    // Sleep for 10 seconds
    sleep(10);
}

// If this is reached, then the 'break' 
// was triggered from inside the while loop

// So here we can log, or perform any other tasks
// we need without actually being dependent on the 
// browser.
于 2013-09-30T03:07:03.743 回答