|
HTML
- <script>
- var source = new EventSource("./msg.php");
- source.onmessage = function(event) {
- document.getElementById("result").innerHTML += event.data + "<br>";
- };
- </script>
- <div id="result"></div>
复制代码 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();
- ?>
复制代码 服务端通过TCP长连接向客户端实实传送数据
|
|