服务器发送的事件工作,但具有大量的时间延迟 [英] Server sent events work, but with a massive time delay
问题描述
伊利诺伊州说这工作完全在我的本地机器上启动关闭时,JS下面的例子连接到stream.php并接收服务器的持续更新当前时间每一秒。
Ill start off by saying this works perfectly on my local machine, the js example below connects to stream.php and receives a continuous update of the servers current time every second.
var source = new EventSource("stream.php");
source.addEventListener('message', function(e) {
console.log(e);
}, false);
source.addEventListener('open', function(e) {
console.log(e);
}, 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);
}
我也希望有点延迟上传到现场后开发。服务器。但存在的约15至20分钟的时间延迟。之前,我连看的第一项。
I did expect a bit of a delay after uploading to the live dev. server. But there is a time delay of about 15 to 20 min. before I even see the first entry.
连接不下降。 (习题已经持续40分钟。现在+)。这只是一个Apache循环的问题(意味着它的时间来看看网络接口)或者是有什么我可以做来解决这个问题?
The connection does not drop. (Prob. been going 40 min. + now.) Is this just an Apache looping problem (means it time to look at web sockets) or is there something I can do to fix this?
推荐答案
Server.php需要如下:
Server.php needs to be as follows:
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);
}
这篇关于服务器发送的事件工作,但具有大量的时间延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!