1 //javascript code 2 3 var source = new EventSource("./main.php"); 4 5 source.onmessage=function(event){ 6 7 document.write(event.data); 8 9 }
1 //main.php 2 3 <?php 4 header('Content-Type: text/event-stream');//服务端推送事件特定的MIME类型 5 header('Cache-Control: no-cache');//禁止缓存 6 echo 'retry:1000'.PHP_EOL;//推送时间(每多少毫秒推送一次, 默认为3000ms/次) 7 8 echo "data:"; 9 echo Time()."<br/>"; 10 echo "\n\n"; 11 12 ?>
来源:https://www.cnblogs.com/weihexinCode/p/12317886.html