https://www.runoob.com/html/html5-serversentevents.html
允许网页获得来自服务器的更新。类似设置回调函数。
c
if(typeof(EventSource)!=="undefined"){
var source=new EventSource("demo_sse.php");
source.onmessage=function(event){
document.getElementById("result").innerHTML+=event.data + "<br>";
};
}
else{
document.getElementById("result").innerHTML="抱歉,你的浏览器不支持 server-sent 事件...";
}
demo_sse.php
c
<?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();
?>
demo_sse.aspx
c
<%@ Page Language="C#" %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e){
Response.ContentType = "text/event-stream";
Response.Expires = -1;
Response.Write("data:" + DateTime.Now + "\n\n");
Response.Flush();
}
</script>