Ai.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. //// 确保在刷新或者调用ob_end_flush()函数后,数据立即被发送到浏览器
  3. //if (function_exists('apache_setenv')) {
  4. // apache_setenv('no-gzip', '1');
  5. //}
  6. //
  7. //ini_set('zlib.output_compression', '0');
  8. //ini_set('implicit_flush', '1');
  9. //
  10. //for ($i = 0; $i < ob_get_level(); $i++) { ob_end_flush(); }
  11. //ob_implicit_flush(1);
  12. //
  13. //// 发送适当的HTTP头信息
  14. //header('Content-Type: text/event-stream');
  15. //header('Cache-Control: no-cache');
  16. //header('X-Accel-Buffering: no'); // 如果使用Nginx,确保Nginx不会缓冲此请求
  17. //$i = 0;
  18. //while ($i < 5) {
  19. // $i++;
  20. // $time = date('Y-m-d H:i:s');
  21. // echo "data: {$time}\n\n";
  22. // flush();
  23. // sleep(1);
  24. //}
  25. //?>
  26. <?php
  27. // 确保在刷新或者调用ob_end_flush()函数后,数据立即被发送到浏览器
  28. if (function_exists('apache_setenv')) {
  29. apache_setenv('no-gzip', '1');
  30. }
  31. ini_set('zlib.output_compression', '0');
  32. ini_set('implicit_flush', '1');
  33. while (ob_get_level() > 0) {
  34. if (!ob_end_flush()) {
  35. break;
  36. }
  37. }
  38. ob_implicit_flush(1);
  39. // 发送适当的HTTP头信息
  40. header('Content-Type: text/event-stream');
  41. header('Cache-Control: no-cache');
  42. header('X-Accel-Buffering: no'); // 如果使用Nginx,确保Nginx不会缓冲此请求
  43. $i = 0;
  44. while ($i < 5) {
  45. $i++;
  46. $time = date('Y-m-d H:i:s');
  47. echo "data: {$time}\n\n";
  48. flush();
  49. sleep(1);
  50. }
  51. ?>