H5接收服务器的推送(Server-Sent)
更新:HHH 时间:2023-1-7
|
- js代码:
<script>
if(typeof(EventSource)!=="undefined"){
var source=new EventSource("test.php");
source.onmessage=function(event)
{
console.log(event.data)
};
}else{
console.log('浏览器不支持Server-Sent')
}
</script>
- php代码:
<?php
header('Content-Type: text/event-stream');
$arr = array(
'name'=>'lee'
);
$data = json_encode($arr);
echo "data: {$data}\n\n";
|