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 小时前
2026 AI搜索流量密码:免费GEO监测工具,优化效果看得见
笔记·百度·微信·facebook·新浪微博
阿蒙Amon6 小时前
TypeScript学习-第10章:模块与命名空间
学习·ubuntu·typescript
AI绘画哇哒哒6 小时前
【干货收藏】深度解析AI Agent框架:设计原理+主流选型+项目实操,一站式学习指南
人工智能·学习·ai·程序员·大模型·产品经理·转行
人良爱编程7 小时前
Hugo的Stack主题配置记录03-背景虚化-导航栏-Apache ECharts创建地图
前端·javascript·apache·echarts·css3·html5
戌中横7 小时前
JavaScript——预解析
前端·javascript·学习
●VON8 小时前
React Native for OpenHarmony:2048 小游戏的开发与跨平台适配实践
javascript·学习·react native·react.js·von
山岚的运维笔记8 小时前
SQL Server笔记 -- 第18章:Views
数据库·笔记·sql·microsoft·sqlserver
ZH15455891318 小时前
Flutter for OpenHarmony Python学习助手实战:自动化脚本开发的实现
python·学习·flutter
xcLeigh8 小时前
Python入门:Python3 requests模块全面学习教程
开发语言·python·学习·模块·python3·requests
xcLeigh8 小时前
Python入门:Python3 statistics模块全面学习教程
开发语言·python·学习·模块·python3·statistics