javascript - 为什么在没有循环或没有下一次调用给 PHP 文件的情况下不断打印这些值?

标签 javascript php json html server-sent-events

以下是“服务器发送事件(SSE)”的演示示例:

HTML 代码(index.html) :

<!DOCTYPE html>
<html>
<body>
<h1>Getting server updates</h1>
<div id="result"></div>

<script>
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...";
}
</script>

</body>
</html>

PHP 代码(demo_sse.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();
?>

我得到的输出如下:

Getting server updates
The server time is: Sun, 31 May 2015 15:27:00 +0530
The server time is: Sun, 31 May 2015 15:27:05 +0530
The server time is: Sun, 31 May 2015 15:27:10 +0530
The server time is: Sun, 31 May 2015 15:27:15 +0530
The server time is: Sun, 31 May 2015 15:27:20 +0530
The server time is: Sun, 31 May 2015 15:27:25 +0530
The server time is: Sun, 31 May 2015 15:27:30 +0530
The server time is: Sun, 31 May 2015 15:27:35 +0530
The server time is: Sun, 31 May 2015 15:27:40 +0530
The server time is: Sun, 31 May 2015 15:27:46 +0530
The server time is: Sun, 31 May 2015 15:27:51 +0530
The server time is: Sun, 31 May 2015 15:27:56 +0530
The server time is: Sun, 31 May 2015 15:28:01 +0530
The server time is: Sun, 31 May 2015 15:28:06 +0530
The server time is: Sun, 31 May 2015 15:28:11 +0530
The server time is: Sun, 31 May 2015 15:28:16 +0530
The server time is: Sun, 31 May 2015 15:28:21 +0530
The server time is: Sun, 31 May 2015 15:28:26 +0530
The server time is: Sun, 31 May 2015 15:28:31 +0530
The server time is: Sun, 31 May 2015 15:28:36 +0530
.
.
.
and so on....

所以我的问题是我已经编写了一个 PHP 代码来打印一次服务器时间,那么为什么在一定的不规则时间间隔之后输出继续打印?

谢谢。

最佳答案

来自手册EventSource

The EventSource interface is used to receive server-sent events. It connects to a server over HTTP and receives events in text/event-stream format without closing the connection.

reconnection time
This is a time, in milliseconds, used to determine how long to wait after a failed attempt to connect before trying again.

当您查看服务器的日志文件时,您会看到客户端每隔几秒连接一次以重新建立连接。发生这种情况是因为 PHP 脚本在完成时关闭了连接。

当你添加 sleep到脚本的末尾,例如

$time = date('r');
echo "data: The server time is: $time\n\n";
flush();
sleep(600);

连接会保持打开状态一段时间。然后,客户端只是等待新消息,不会尝试重新连接到服务器。

关于javascript - 为什么在没有循环或没有下一次调用给 PHP 文件的情况下不断打印这些值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30556150/

相关文章:

javascript - 使用 != 和 not-in 查询会在 firestore 中使用 Query.where 抛出错误

javascript - 私有(private)方法和特权方法的区别

PHP - 获取 mysqli 数据以与 css 并排显示

javascript - 带有替换函数的 JSON.stringify 中的奇怪行为

c# - 将字典对象转换成Json字符串

JavaScript 类扩展了 onclick 函数

javascript - 如何将 querySelectorAll() 函数添加到 IE <= 7 的元素?

php - mysql 列用于插入和不同的列用于更新

php - 随机 "PHP Fatal error: Out of memory"错误

ios - 使用我创建的 .json 数据库