douwan7382 2016-09-27 22:55
浏览 56
已采纳

SSE在脚本执行完之前不发送事件?

I am having a issue where SSE events wait until the script has finished executing before finally sending the data, after hours of searching for a plausible explanation or solution, I have found nothing.

I have found a example however that works on that website, however taking that code onto my server, it doesn't work. (URL: http://www.binarytides.com/monitor-progress-long-running-php-scripts-html5-server-sent-events/)

In this example I have noticed a header of Transfer-Encoding: chunked When I had tried to make Apache send this header through .htaccess with Header set, the response code turned to "(failed)".

I also noticed the example didn't have a Content-Length, where as the one from my server does.

This was all tested on Chrome v.53.0.2785.116m

After that I decided to take it to the stacks.

Here is a example:

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

    function sendMsg($id, $msg) {
        echo "id: $id" . PHP_EOL;
        echo "data: $msg" . PHP_EOL;
        echo PHP_EOL;
        ob_flush();
        flush();
    }

    while ($x < 5) {
        $serverTime = time();
        sendMsg($serverTime, 'server time: ' . date("h:i:s", time()));
        sleep(1);
        $x++;
    }
?>

If I were to perform a request to this script, I would get these headers:

HTTP/1.1 200 OK
Date: Tue, 27 Sep 2016 22:34:11 GMT
Server: Apache
Cache-Control: no-cache
Vary: Accept-Encoding,User-Agent
Content-Encoding: gzip
Content-Length: 91
Keep-Alive: timeout=15, max=43
Connection: Keep-Alive
Content-Type: text/event-stream

I will not get any data from this until the script had finished executing.

At this point I'm unclear if its Apache, or PHP that is at fault here, but from what I can tell the server is at fault. I've tried version of PHP ranging from 5.5-7, all with no luck.

Any ideas on whats going wrong here and how to fix it?

EDIT 1: I tried the same script on another server and it had worked properly, and sent the following headers:

HTTP/1.1 200 OK
Server: nginx
Date: Wed, 28 Sep 2016 15:40:00 GMT
Content-Type: text/event-stream;charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
Keep-Alive: timeout=15
Cache-Control: no-cache
ngpass_ngall: 1
  • 写回答

1条回答 默认 最新

  • doushou7169 2016-09-28 21:38
    关注

    I suspect the problem is gzip-ing the output. (Your working nginx server is not doing that.)

    In your .htaccess file, can you try both of these:

    php_flag zlib.output_compression off
    SetEnv no-gzip 1
    

    (Borrowed from https://webmasters.stackexchange.com/q/61258/18151 and its answers.)

    BTW, you shouldn't need to add the chunking header yourself; it will be set if the output is chunked.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?