gpt4 book ai didi

html - 3-4 秒后收到服务器发送的事件

转载 作者:行者123 更新时间:2023-12-02 09:23:43 25 4
gpt4 key购买 nike

this page 读取有关服务器发送事件的信息时,我对事件的时间安排感到困惑。基本上,example shown有一个 PHP 脚本将系统时间发送到网页:

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

$time = date('r');
echo "data: The server time is: {$time}\n\n";
flush();
?>

当页面接收并呈现它时:

if(typeof(EventSource) !== "undefined") {
var source = new EventSource("demo_sse.php");
source.onmessage = function(event) {
document.getElementById("result").innerHTML += event.data + "<br>";
};
} else {
document.getElementById("result").innerHTML = "Sorry, your browser does not support server-sent events...";
}

我的困惑是服务器似乎每 3-4 秒发送一次消息:

The server time is: Tue, 20 Sep 2016 11:55:12 -0400
The server time is: Tue, 20 Sep 2016 11:55:16 -0400
The server time is: Tue, 20 Sep 2016 11:55:20 -0400
The server time is: Tue, 20 Sep 2016 11:55:23 -0400
The server time is: Tue, 20 Sep 2016 11:55:28 -0400
The server time is: Tue, 20 Sep 2016 11:55:32 -0400
The server time is: Tue, 20 Sep 2016 11:55:35 -0400
The server time is: Tue, 20 Sep 2016 11:55:39 -0400
The server time is: Tue, 20 Sep 2016 11:55:43 -0400
The server time is: Tue, 20 Sep 2016 11:55:46 -0400
The server time is: Tue, 20 Sep 2016 11:55:50 -0400
The server time is: Tue, 20 Sep 2016 11:55:53 -0400
The server time is: Tue, 20 Sep 2016 11:55:57 -0400
The server time is: Tue, 20 Sep 2016 11:56:01 -0400
The server time is: Tue, 20 Sep 2016 11:56:04 -0400
The server time is: Tue, 20 Sep 2016 11:56:08 -0400
The server time is: Tue, 20 Sep 2016 11:56:12 -0400
The server time is: Tue, 20 Sep 2016 11:56:15 -0400

但是,我在服务器端或客户端都没有看到这种延迟。是网站服务器和我的浏览器之间的网络延迟吗?还是其他原因?

最佳答案

默认重试时间为 3 秒,如 http://www.html5rocks.com/en/tutorials/eventsource/basics/ 所示查看该链接上的“控制重新连接超时部分”。您可以通过发送一行 retry:100 来自定义它强制执行仅 100 毫秒的重试时间间隔。

关于html - 3-4 秒后收到服务器发送的事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39598846/

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