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>
相关推荐
minglie11 小时前
高等代数试题
学习
Xiaofeng36932 小时前
GPT-5.6 发布后,我用了一个周末重新规划学习路线图
gpt·学习
其实防守也摸鱼3 小时前
运维--怎么看接口的请求和返回
运维·学习·网络安全·网络攻击模型·burpsuite·攻防对抗·蓝队
酷讯网络_2408701604 小时前
区块粮仓宠物NFT源码区块狗/抢购转让预约区块投资理财系统
学习·开源·宠物
疯狂打码的少年4 小时前
【软件工程】软件开发模型:增量模型与迭代模型
笔记·软件工程
Generalzy4 小时前
从本地 Demo 到生产级检索:Milvus 学习笔记(3)
笔记·学习·milvus
心中有国也有家5 小时前
AtomGit Flutter 鸿蒙客户端:ModalBottomSheet 实战
android·javascript·学习·flutter·华为·harmonyos
tyqtyq225 小时前
求职信生成:AI 智能求职信撰写系统的鸿蒙实现
人工智能·学习·华为·生活·harmonyos
MartinYeung55 小时前
[论文学习]PrivacyLens:评估语言模型在行动中的隐私规范意识
人工智能·学习·语言模型
凉、介6 小时前
Virtio 系列(一):框架概览
笔记·学习·嵌入式·虚拟化·virtio