html5学习笔记19-SSE服务器发送事件(Server-Sent Events)

https://www.runoob.com/html/html5-serversentevents.html

允许网页获得来自服务器的更新。类似设置回调函数。

if(typeof(EventSource)!=="undefined"){
	var source=new EventSource("demo_sse.php");
    source.onmessage=function(event){
        document.getElementById("result").innerHTML+=event.data + "
"
; }; } else{ document.getElementById("result").innerHTML="抱歉,你的浏览器不支持 server-sent 事件..."; }

demo_sse.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();
?>

demo_sse.aspx

<%@ 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>

你可能感兴趣的:(html,html5,学习,笔记)