gpt4 book ai didi

php - 服务器发送的事件有效,但时间延迟很大

转载 作者:可可西里 更新时间:2023-11-01 12:49:58 25 4
gpt4 key购买 nike

首先我会说这在我的本地机器上完美运行,下面的 js 示例连接到 stream.php 并每秒接收服务器当前时间的持续更新。

索引.php

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);

流.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 分钟的时间延迟。在我看到第一个条目之前。

连接不会断开。 (可能已经进行了 40 分钟以上。)这只是一个 Apache 循环问题(意味着是时候查看网络套接字了)还是我可以做些什么来解决这个问题?

最佳答案

Server.php需要如下:

流.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);
}

关于php - 服务器发送的事件有效,但时间延迟很大,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12297740/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com