脚本宝典收集整理的这篇文章主要介绍了html5 + php 实现实时推送消息,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
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>
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}nn";
$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}nn";
}
ob_flush();//刷新
flush();//刷新
?>
以上是脚本宝典为你收集整理的html5 + php 实现实时推送消息全部内容,希望文章能够帮你解决html5 + php 实现实时推送消息所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。