服务器发送的事件不断重新启动新请求

Server Sent Events keeps restarting with new request

我有一个非常简单的 PHP 服务器端事件控制器(针对这个问题进行了简化):

    $response = new StreamedResponse(function() {
        while (true) {

            if(connection_status() != CONNECTION_NORMAL) {
                die();
            };

            echo 'data: '. "\n\n";

            ob_end_flush();
            flush();

            sleep(1);
        }

    });

    $response->headers->set('Cache-Control', 'no-cache');
    $response->headers->set('Content-Type', 'text/event-stream');
    return $response;

当我从我的 Angular 应用程序调用它时,它会打开和关闭每个循环的连接,所以它看起来像这样:

有些事情发生了变化,因为之前我只看到一个请求和该请求的一个旋转图标,然后数据流每秒更新一次。

什么可能导致这种情况?

我删除了 ob_end_flush();,它保持连接按预期打开。