iframe彗星出口关闭

时间:2013-09-30 03:04:41

标签: php apache comet

您好我正在使用标准的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);
        }

1 个答案:

答案 0 :(得分: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.