Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/262.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 既然没有循环或没有对PHP文件进行下一次调用,为什么会连续打印这些值?_Javascript_Php_Json_Html_Server Sent Events - Fatal编程技术网

Javascript 既然没有循环或没有对PHP文件进行下一次调用,为什么会连续打印这些值?

Javascript 既然没有循环或没有对PHP文件进行下一次调用,为什么会连续打印这些值?,javascript,php,json,html,server-sent-events,Javascript,Php,Json,Html,Server Sent Events,以下是“服务器发送事件(SSE)”的演示示例: HTML代码(index.HTML): 所以我的问题是,我已经编写了一个PHP代码来打印服务器时间,那么为什么在某个不规则的时间间隔之后,输出会继续打印 谢谢。来自手册 EventSource接口用于接收服务器发送的事件。它通过HTTP连接到服务器,并以文本/事件流格式接收事件,而不关闭连接 及 重新连接时间 这是一个以毫秒为单位的时间,用于确定尝试连接失败后等待多长时间后再重试 当您查看服务器的日志文件时,您将看到客户端每隔几秒钟连接一次以重新建

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

HTML代码(index.HTML):

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

谢谢。

来自手册

EventSource接口用于接收服务器发送的事件。它通过HTTP连接到服务器,并以文本/事件流格式接收事件,而不关闭连接

重新连接时间
这是一个以毫秒为单位的时间,用于确定尝试连接失败后等待多长时间后再重试

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

在脚本末尾添加时,例如

$time=date('r');
echo“数据:服务器时间为:$time\n\n”;
冲洗();
睡眠(600);

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

为什么要使用
flush()
demo_sse.php
?@nhee:将输出数据刷新回网页。尽快将消息推送到客户端。
<!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
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....