服务器发送的事件直到 PHP 脚本执行结束才发送

Server sent events not send until PHP script execution end

我已经成功使用 server sent events 几个月了。突然它停止工作。现在在服务器完成执行之前不会收到任何事件,然后会立即引发所有事件。

由于项目在不断开发中,我从头开始尝试,但没有成功。我的客户端代码是:

<!DOCTYPE html>
<html>
<head>
    <title>HTML5 Server-Sent Events</title>
    <script type="text/javascript">
    window.onload = function()
    {
        var source = new EventSource("ServerSentEvents.php");
        source.onmessage = function(event) {
            document.getElementById("result").innerHTML += event.data + "<br>";
        };
        source.onerror = function(event) {
            console.log(event);
        };
    };
    </script>
</head>
<body>
    <p>Here we go.</p>
    <div id="result">

    </div>
</body>
</html>

我的服务器代码是:

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

for ($i = 0; $i < 20; $i++)
{
    $time = date('r');
    echo "data: The server time is: {$time}\n\n";

    ob_flush();
    flush();
    usleep(500000);
}
?>

我想,我正在使用基本 XAMPP 安装。我没有使用任何第三方库。

以防万一,这是我的 httpd.conf:

ServerRoot "C:/xampp/apache"

Listen 127.0.0.1:80

LoadModule access_compat_module modules/mod_access_compat.so
LoadModule actions_module modules/mod_actions.so
LoadModule alias_module modules/mod_alias.so
LoadModule allowmethods_module modules/mod_allowmethods.so
LoadModule asis_module modules/mod_asis.so
LoadModule auth_basic_module modules/mod_auth_basic.so
LoadModule authn_core_module modules/mod_authn_core.so
LoadModule authn_file_module modules/mod_authn_file.so
LoadModule authz_core_module modules/mod_authz_core.so
LoadModule authz_groupfile_module modules/mod_authz_groupfile.so
LoadModule authz_host_module modules/mod_authz_host.so
LoadModule authz_user_module modules/mod_authz_user.so
LoadModule autoindex_module modules/mod_autoindex.so
LoadModule cgi_module modules/mod_cgi.so
LoadModule dav_lock_module modules/mod_dav_lock.so
LoadModule dir_module modules/mod_dir.so
LoadModule env_module modules/mod_env.so
LoadModule headers_module modules/mod_headers.so
LoadModule include_module modules/mod_include.so
LoadModule info_module modules/mod_info.so
LoadModule isapi_module modules/mod_isapi.so
LoadModule log_config_module modules/mod_log_config.so
LoadModule cache_disk_module modules/mod_cache_disk.so
LoadModule mime_module modules/mod_mime.so
LoadModule negotiation_module modules/mod_negotiation.so
LoadModule proxy_module modules/mod_proxy.so
LoadModule proxy_ajp_module modules/mod_proxy_ajp.so
LoadModule rewrite_module modules/mod_rewrite.so
LoadModule setenvif_module modules/mod_setenvif.so
LoadModule socache_shmcb_module modules/mod_socache_shmcb.so
LoadModule ssl_module modules/mod_ssl.so
LoadModule status_module modules/mod_status.so
LoadModule version_module modules/mod_version.so

LoadModule  proxy_http_module    modules/mod_proxy_http.so
LoadModule  deflate_module       modules/mod_deflate.so
LoadModule  xml2enc_module       modules/mod_xml2enc.so
LoadModule  proxy_html_module    modules/mod_proxy_html.so

<IfModule unixd_module>
User daemon
Group daemon

</IfModule>

ServerAdmin postmaster@localhost

ServerName localhost:80

<Directory />
    AllowOverride none
    Require all denied
</Directory>

DocumentRoot "d:/Data/WWW/"
<Directory "d:/Data/WWW/">
    Options Indexes FollowSymLinks Includes ExecCGI

    AllowOverride All

    Require all granted
</Directory>

<IfModule dir_module>
    DirectoryIndex index.php index.pl index.cgi index.asp index.shtml index.html index.htm \
                   default.php default.pl default.cgi default.asp default.shtml default.html default.htm \
                   home.php home.pl home.cgi home.asp home.shtml home.html home.htm
</IfModule>

<Files ".ht*">
    Require all denied
</Files>

ErrorLog "logs/error.log"
LogLevel warn

<IfModule log_config_module>
    LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\"" combined
    LogFormat "%h %l %u %t \"%r\" %>s %b" common

    <IfModule logio_module>
      LogFormat "%h %l %u %t \"%r\" %>s %b \"%{Referer}i\" \"%{User-Agent}i\" %I %O" combinedio
    </IfModule>
    CustomLog "logs/access.log" combined
</IfModule>

<IfModule alias_module>
    ScriptAlias /cgi-bin/ "C:/xampp/cgi-bin/"

</IfModule>

<IfModule cgid_module>
</IfModule>

<Directory "C:/xampp/cgi-bin">
    AllowOverride All
    Options None
    Require all granted
</Directory>



<IfModule mime_module>
    TypesConfig conf/mime.types

    AddType application/x-compress .Z
    AddType application/x-gzip .gz .tgz

    AddHandler cgi-script .cgi .pl .asp

    AddType text/html .shtml
    AddOutputFilter INCLUDES .shtml
</IfModule>

<IfModule mime_magic_module>
    MIMEMagicFile "conf/magic"
</IfModule>

Include conf/extra/httpd-mpm.conf

Include conf/extra/httpd-multilang-errordoc.conf

Include conf/extra/httpd-autoindex.conf

Include conf/extra/httpd-languages.conf

Include conf/extra/httpd-userdir.conf

Include conf/extra/httpd-info.conf

Include conf/extra/httpd-vhosts.conf
Include "conf/extra/httpd-proxy.conf"
Include "conf/extra/httpd-default.conf"
Include "conf/extra/httpd-xampp.conf"

<IfModule proxy_html_module>
Include conf/extra/proxy-html.conf
</IfModule>

Include conf/extra/httpd-ssl.conf
#
<IfModule ssl_module>
SSLRandomSeed startup builtin
SSLRandomSeed connect builtin
</IfModule>

AcceptFilter http none

<IfModule mod_proxy.c>
<IfModule mod_proxy_ajp.c>
Include "conf/extra/httpd-ajp.conf"
</IfModule>
</IfModule>

谢谢。

我几乎不敢相信。白白测试一天,大喊大叫心情极差

只能怪一件事(或程序)。几天前我安装的防病毒软件 (BitDefender 2015),当时我的儿子感染了 Locker 病毒。

我试过把网页加入白名单,没办法,试过关掉,没办法,最后卸载了,还好。

我在这里发帖是为了让遇到同样问题的人头疼不已。

我的问题原来是我的防病毒软件 - BitDefender Internet Security 2015。 升级到 BitDefender 2017 解决了这个问题...