1、html页面basic_sse.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>数据推送</title> </head> <body> <pre id="result"> Initializing... </pre> <script type="text/javascript"> if(typeof(EventSource)!=="undefined") { var source = new EventSource("/sjts/index.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>
2、php页面index.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"; $mysqli = new MySQLi('localhost','root','','test'); $sql = 'select ac_id from article_class where ac_name="11";'; $result = $mysqli->query($sql); while($row = $result->fetch_assoc()){ $time = $row['ac_id']; echo "data: The server time is: {$time}\n\n"; } ob_flush();//刷新 flush();//刷新 ?>
总结:通过改变数据库ac_id自动,前端不刷新即可实施改变数据