PHP 服务器发送事件连接不会关闭? [英] PHP Server Sent Events Connection won't close?

查看:63
本文介绍了PHP 服务器发送事件连接不会关闭?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现了一个服务器发送的事件在我的 Web 应用程序上使用 eventsource.基本上在 javascript 我的代码看起来像:

I have implemented a server sent events with eventsource on my web application. Basically in javascript my code look like :

    var myEventSource;
    if (typeof(EventSource) !== "undefined" && !myJsIssetFunction(viridem.serverSideEvent.config.reindexProcessingEvent)) {
        myEventSource = new EventSource('/my/url/path.php?event=myevent');
        EventSource.onmessage = function(e) {
          [...] //Dealing with e.data that i received ...
        }
    }

在 PHP 方面我有这样的东西:

on the PHP side I have something Like this :

<?php
  header('Content-Type: text/event-stream');
  header('Cache-Control: no-cache');
  header("Access-Control-Allow-Origin: *");

  //this or set_the_limit don't work but whatever I can deal without it
  ini_set('max_execution_time', 300);
  //ignore_user_abort(true); tried with true and false

  bool $mustQuit = false;

  while (!$mustQuit && connection_status() == CONNECTION_NORMAL) {
     if(connection_aborted()){
      exit();
     }
     [...] //doing some checkup

    if ($hasChange) {
      //Output stuffs
      echo 'data:';
      echo json_encode($result);
      echo "\n\n";
      ob_flush();
      flush();
      sleep(5);
    }

  }

从在以下位置找到的答案:PHP 事件源不断执行,文本/event-stream" 标头应该使连接自动关闭,但在我的情况下不会..

from the answer found at : PHP Event Source keeps executing , the "text/event-stream" headers should make the connection close automatically but it doesn't in my case ..

我确实在 window.onbeforeunload 事件中添加了一个 eventsource.close 但它没有关闭该事件.

I did add an eventsource.close in the window.onbeforeunload event but it didn't close the event.

window.onbeforeunload =  function() {
    myEventSource.close();
    myEventSource = null;
};

如果我查看浏览器的网络部分,我可以看到标题是(添加最大循环 30 后):内容类型:文本/事件流;字符集=UTF-8

If I look the network section of my browser I can see the Headers are (after adding maximum loop of 30) : Content-Type: text/event-stream;charset=UTF-8

响应头:

访问控制允许来源:*

缓存控制:无缓存

连接:保持活动

内容类型:文本/事件流;字符集=UTF-8

Content-Type: text/event-stream;charset=UTF-8

服务器:Apache/2.4.18 (Ubuntu)

Server: Apache/2.4.18 (Ubuntu)

日期:2018 年 4 月 26 日星期四 20:29:46 GMT

Date: Thu, 26 Apr 2018 20:29:46 GMT

到期:格林威治标准时间 1981 年 11 月 19 日星期四 08:52:00

Expires: Thu, 19 Nov 1981 08:52:00 GMT

请求头:

连接:保持活动

接受:文本/事件流

缓存控制:无缓存

注意:我确认脚本仍在使用日志运行,并通过 bash (ps -ax | grep -c apache2) 检查 apache2 进程,这些进程总是在增加.

Note : I confirm that the script is still running with logs and by checking apache2 process with bash (ps -ax | grep -c apache2) that are always incrementing.

推荐答案

感谢@LawrenceCherone 的帮助,我确实发现您需要输出数据"对于 connection_aborted 工作...

Thanks to @LawrenceCherone help, I did find out that you need to "output data" for connection_aborted to work...

就我而言,我只在需要时才输出数据......

In my case I was outputing data only when I needed to ...

通过添加

   if ($hasChange) {
      //Output stuffs
      echo 'data:';
      echo json_encode($result);
      echo "\n\n";
      ob_flush();
      flush();
      sleep(5);

    } else {
       echo 'data:';
       echo "\n\n";
       ob_flush();
       flush();
       if(connection_aborted()){
         exit();
       }
       sleep(5);
    }

connection_aborted 开始工作.

connection_aborted started working.

这篇关于PHP 服务器发送事件连接不会关闭?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆