我开始说这在我的本地机器上完美运行,下面的js示例连接到stream.PHP并且每秒接收服务器当前时间的连续更新.
的index.PHP
var source = new EventSource("stream.PHP"); source.addEventListener('message',function(e) { console.log(e); },false); source.addEventListener('open',false); source.addEventListener('error',function(e) { if (e.readyState == EventSource.CLOSED) { console.log('closed'); } },false);
stream.PHP
while(true) { // Headers must be processed line by line. header('Content-Type: text/event-stream'); header('Cache-Control: no-cache'); // Set data line print "data: " . date( 'G:H:s',time() ) . PHP_EOL . PHP_EOL; // Toilet flush(); // Wait one second. sleep(1); }
上传到live dev之后我确实预计会有一点延迟.服务器.但是有大约15到20分钟的时间延迟.在我看到第一个条目之前.
连接不会丢失. (问题.现在已经过了40分钟.)这只是一个Apache循环问题(意味着是时候看看网络套接字了)还是我可以做些什么来解决这个问题?
Server.PHP需要如下:
原文链接:https://www.f2er.com/php/130396.htmlstream.PHP
while(true) { // Headers must be processed line by line. header('Content-Type: text/event-stream'); header('Cache-Control: no-cache'); // Set data line print "Event: server-time" . PHP_EOL; print "data: " . date( 'G:H:s',time() ) . PHP_EOL; print PHP_EOL; ob_end_flush(); // Strange behavIoUr,will not work flush(); // Unless both are called ! // Wait one second. sleep(1); }