Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/373.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 简单服务器发送事件:30分钟后时钟停止_Javascript_Php_Polling_Server Sent Events - Fatal编程技术网

Javascript 简单服务器发送事件:30分钟后时钟停止

Javascript 简单服务器发送事件:30分钟后时钟停止,javascript,php,polling,server-sent-events,Javascript,Php,Polling,Server Sent Events,我会用发送事件制作一个简单的时钟,我的代码是: 客户端和服务器位于局域网上,浏览器为iceweasel v.38.7.1 php: 时钟停止后工作30-45分钟, 我哪里做错了 谢谢。您的PHP脚本应该如下所示: <?php header("Content-Type: text/event-stream"); header("Cache-Control: no-cache"); header("Connection: keep-alive"); function sendMsg($msg)

我会用发送事件制作一个简单的时钟,我的代码是: 客户端和服务器位于局域网上,浏览器为iceweasel v.38.7.1

php:

时钟停止后工作30-45分钟,
我哪里做错了

谢谢。

您的PHP脚本应该如下所示:

<?php
header("Content-Type: text/event-stream");
header("Cache-Control: no-cache");
header("Connection: keep-alive");
function sendMsg($msg) {
  echo "retry: 1000\n";
  echo "data: $msg" .PHP_EOL;
  echo PHP_EOL;
  ob_flush();
  flush();
}
while(true){
   sendMsg(date("H:i", time()));
   sleep(1);
   }

同样,这意味着您希望PHP脚本永远运行,无限循环是经过设计的,不是错误。

您可以使用开发人员控制台进行调试。通过查看代码很难找到问题
(function get_time()
{var source = new EventSource('/config/rtc/get_time.php');

source.addEventListener('message',function (event)
  {document.getElementById("orologio").innerHTML=event.data;},false);

source.addEventListener('error', function(event) { 
if (event.readyState === EventSource.CLOSED)
{get_time();}},false);
})();
<?php
header("Content-Type: text/event-stream");
header("Cache-Control: no-cache");
header("Connection: keep-alive");
function sendMsg($msg) {
  echo "retry: 1000\n";
  echo "data: $msg" .PHP_EOL;
  echo PHP_EOL;
  ob_flush();
  flush();
}
while(true){
   sendMsg(date("H:i", time()));
   sleep(1);
   }
set_time_limit(0);