источник событий — PHP и события — сброс до клиента не происходит, пока сценарий не умирает

Я пытаюсь создать простую страницу, которая отправляет события на веб-страницу, но я не могу заставить PHP отправлять вывод до завершения страницы.

Я использую PHP-FPM и php5.6.27.

это моя простая HTML-страница:

<html>
<head>
<title>test events</title>
</head>
<body>
testing events
<ul id="pingEventList" style="float: left"></ul>
<ul id="messageEventList" style="float: left"></ul>
<script>
var evtSource=new EventSource("./s_events.php?auth=e3b164ef33d802c45da829b8f1240d16");
var pingEventList=document.getElementById('pingEventList');
var messageEventList=document.getElementById('messageEventList');
evtSource.onmessage=function (e){
var newElement=document.createElement("li");
newElement.innerHTML="message: "+e.data;
messageEventList.appendChild(newElement);
};
evtSource.addEventListener("initial", function (e){
var newElement=document.createElement("li");
//              var obj=JSON.parse(e.data);
newElement.innerHTML="initial info "+e.data;
console.log("initial info "+e.data);
pingEventList.appendChild(newElement);
}, false);
evtSource.addEventListener("modified", function (e){
var newElement=document.createElement("li");
//              var obj=JSON.parse(e.data);
newElement.innerHTML="modified info "+e.data;
console.log("modified info "+e.data);
pingEventList.appendChild(newElement);
}, false);
evtSource.onerror=function (e){
console.log("EventSource failed.", e);
//              while(pingEventList.firstChild){
//                  pingEventList.removeChild(pingEventList.firstChild);
//              }
//              alert("EventSource failed.");
};
//          evtSource.close();
</script>
</body>
</html>

и это моя страница PHP:

<?php

@set_time_limit(0); // Disable time limit
// Prevent buffering
if(function_exists('apache_setenv')){
@apache_setenv('no-gzip', 1);
}
@ini_set('zlib.output_compression', 0);
@ini_set('implicit_flush', 1);
while(ob_get_level() !=0){
ob_end_flush();
}
ob_implicit_flush(1);
ignore_user_abort(false);
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Credentials: true');
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache'); // recommended to prevent caching of event data.
header('X-Accel-Buffering: off'); // Disables FastCGI Buffering on Nginx
$sleep_time          = 1; //0.5  // seconds to sleep after the data has been sent
$exec_limit_time     = 15; //600;  // the time limit of the script in seconds
$keep_alive_time     = 30; //300;  // The interval of sending a signal to keep the connection alive
$client_reconnect    = 1;  // the time client to reconnect after connection has lost in seconds
$keep_alive_start    = time();
$exec_limit_start    = time();$ts_last_used    = intval(isset($_SERVER["HTTP_LAST_EVENT_ID"])?$_SERVER["HTTP_LAST_EVENT_ID"]:0);
echo 'retry:'.($client_reconnect*1000)."\n";
$event           = [
'id'     => $ts_last_used++,
'event'  => 'initial',
'data'   => "[\ndata:".'INITIAL DATA'."\ndata:]",
];
_send_event_data($event);
while(1){
// Did the connection fail?
if(connection_status() !=CONNECTION_NORMAL){
break;
}else{
$event = [
'id'     => $ts_last_used++,
'event'  => 'modified',
'data'   => '{"id":"stuff here too"}',
];
_send_event_data($event);
}
// Sleep for x seconds
usleep($sleep_time*1000000);
if($exec_limit_start+$exec_limit_time<time()){
break;
}
if($keep_alive_start+$keep_alive_time<time()){
$keep_alive_start = time();
echo ': '.sha1(mt_rand())."\n\n";
}
}
// If this is reached, then the 'break'
// was triggered from inside the while loop
//
// So here we can log, or perform any other tasks
// we need without actually being dependent on the
// browser.

function _send_event_data($info){
global $keep_alive_start;
if(isset($info['id'])){
echo 'id:'.$info['id']."\n";
}
if(isset($info['event'])){
echo 'event:'.$info['event']."\n";
}
echo 'data:'.$info['data']."\n";
if(!isset($info['more_to_come'])||!$info['more_to_come']){
echo "\n";
}
$keep_alive_start = time();
@ob_end_flush();
flush();
}

Я пробовал различные возможные решения, но я не могу понять это. Я также изменил PHP-FPM на mod_php (я использую apache) на тот случай, если это был PHP_FPM, но у меня ничего не получилось.

Я открыт для предложений

2

Решение

Я понял это … это был apache, который содержал вывод, это моя новая конфигурация для fast-cgi usign php-fpm:

<IfModule fastcgi_module>
#php-fpm
AddHandler php5-fcgi .php .html
Action php5-fcgi /php5-fcgi
Alias /php5-fcgi /usr/local/www/cgi-bin/php5-fcgi
FastCgiExternalServer /usr/local/www/cgi-bin/php5-fcgi -flush -socket /var/run/php-fpm.sock -pass-header Authorization -idle-timeout 600
<Directory /usr/local/www/cgi-bin>
Require all granted
</Directory>
</IfModule>

То, что сделало разницу, было -flush Добавлено в FastCgiExternalServerтеперь вывод больше не буферизуется и выводится сразу (я смог найти ответ благодаря этой странице: http://www.jeffgeerling.com/blog/2016/streaming-php-disabling-output-buffering-php-apache-nginx-and-varnish )

1

Другие решения

Других решений пока нет …

По вопросам рекламы ammmcru@yandex.ru
Adblock
detector