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>
相关推荐
Linux-lucky5 小时前
21-Linux学习之旅之HTTPS和安全加固
linux·运维·学习·ubuntu·https
wp123_16 小时前
硬件元器件笔记|IPX8 防水 Type‑C 母座安费诺 124018802112A 与 TONEVEE TY48086‑24A 分析
c语言·开发语言·笔记
mifengxing7 小时前
操作系统 文件系统
笔记·操作系统·计算机408
学运维的Kysan7 小时前
暑假运维学习打卡第二十六天8.17
学习
无聊的菜鸟8 小时前
TMS320F2806X学习笔记(零)—— C2000、CCS、新建工程
笔记·mcu·c2000·f2806x
dear_bi_MyOnly8 小时前
AI人工智能分类识别——机器如何学习
人工智能·学习·分类
ljt27249606619 小时前
Compose笔记(八十三)--onVisibilityChanged
笔记
启雀AI11 小时前
培训平台移动端离线学习方案设计与实现:视频缓存、断点续传与进度同步的工程实践
android·学习·缓存·音视频·企业lms
Fa_Mian_Tuan12 小时前
图论基础|邻接矩阵超详细讲解(含无向/有向/带权图+完整可运行C语言代码)
c语言·数据结构·笔记·算法·图论
Bruce_Liuxiaowei13 小时前
从零到可运行:基于 Vue3 + FastAPI + DeepSeek-V3 的 AI 英语单词学习系统全栈实战
人工智能·python·学习·fastapi·全栈·智能体