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>
相关推荐
M78佐菲4 小时前
51单片机学习笔记:ds18b20要点整理
linux·笔记·嵌入式硬件·学习·51单片机
高亦真4 小时前
今天是学习嵌入式的第37天
linux·学习·算法
蒸蒸yyyyzwd5 小时前
从面经中学到的三件事
c++·笔记
泡泡鱼(敲代码中)7 小时前
Python 字符串 str 完整学习笔记
python·学习
IT小白杨8 小时前
社媒多账号运营的环境隔离:一张图谱模型,解释清“为什么换IP还是被连坐“
经验分享·笔记·矩阵·指纹浏览器
kyrie_sakura8 小时前
python学习笔记15 -- Anaconda,Jupyter,numpy
python·学习·jupyter·numpy
PC2005-cloud10 小时前
Rust学习笔记:控制流——if、loop、while、for与match
笔记·学习·rust
彧azz11 小时前
DFS与BFS:图遍历的两大核心算法
数据结构·学习·算法·深度优先·广度优先
IT笔记11 小时前
Rust 迭代器多级链式调用执行顺序笔记
开发语言·笔记·rust
kyrie_sakura11 小时前
python学习笔记14 -- FastAPI
笔记·python·学习·fastapi