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

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>
相关推荐
每天题库5 分钟前
交安C证报考条件有哪些?公路水运安全员C证报名时间及考试科目
学习·安全·考试·题库·考证
lifallen33 分钟前
长任务怎样选择遗忘:clearing、compaction 与 memory
人工智能·学习·ai·ai编程
高亦真33 分钟前
今天是学习嵌入式的第35天
linux·学习·算法
️学习的小王34 分钟前
FastAPI核心知识点与高频易错点总结
学习·fastapi
HugoStudio_SWAN1 小时前
洛谷 P1319 / P1320 压缩技术——同一枚硬币的正反面
c++·学习·程序人生·算法
疯狂打码的少年1 小时前
【数据库技术】SQL嵌套查询(IN/EXISTS/ANY/ALL)
数据库·笔记·sql
λqaq72 小时前
MongoDB数据库基础 :非关系型数据库入门与常用操作
数据库·学习·mongodb·nosql
凯尔萨厮2 小时前
Java学习笔记十四(网络通信)
笔记·学习
大模型码小白2 小时前
数据可视化:AI处理多维数据的HTML5可视化方案
大数据·前端·javascript·人工智能·机器学习·信息可视化·html5
AAA代码批发商2 小时前
DAYS 38 TCP并发服务器模型详解
linux·网络·笔记·学习