服務(wù)端主動(dòng)發(fā)送數(shù)據(jù)回客戶端在H5里的完成步奏
發(fā)表時(shí)間:2023-12-24 來(lái)源:明輝站整理相關(guān)軟件相關(guān)文章人氣:
[摘要]我們知道,在Server Sent Event里,通過(guò)EventSource對(duì)象接收服務(wù)器發(fā)送事件的通知是有三個(gè)事件的,message, open, error這三種,今天就給大家演示一下服務(wù)端主動(dòng)發(fā)送數(shù)據(jù)回客戶端在H5里的實(shí)現(xiàn)步奏。Server Sent EventServer Sent Eve...
我們知道,在Server Sent Event里,通過(guò)EventSource對(duì)象接收服務(wù)器發(fā)送事件的通知是有三個(gè)事件的,message, open, error這三種,今天就給大家演示一下服務(wù)端主動(dòng)發(fā)送數(shù)據(jù)回客戶端在H5里的實(shí)現(xiàn)步奏。
Server Sent Event
Server Sent Event通過(guò)EventSource對(duì)象接收服務(wù)器發(fā)送事件的通知. 有三個(gè)事件message, open, error
下面的代碼演示了使用的方法
例子代碼運(yùn)行環(huán)境: node.js
代碼
粘貼下面代碼運(yùn)行node index.js
//index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>server-sent</title>
</head>
<body>
<script>
window.addEventListener("load",function () {
let status = document.getElementById("status");
let output = document.getElementById("output");
let source;
function connect() {
source = new EventSource("stream");//與服務(wù)器端建立連接
//鑒定message事件, 獲取服務(wù)端發(fā)送的數(shù)據(jù)
source.addEventListener("message", function (event) {
output.textContent = event.data;
}, false);
//監(jiān)聽open事件, 判斷連接是否進(jìn)行中
source,addEventListener("open", function (event) {
status.textContent = '連接打開了';
},false);
//監(jiān)聽error事件, 處理連接錯(cuò)誤的情況
source.addEventListener("error", function (event) {
if (event.target.readyState === EventSource.CLOSED) {
source.close();
status.textContent = '連接關(guān)閉了';
} else {
status.textContent = "連接關(guān)閉了!未知錯(cuò)誤!";
}
}, false);
}
if(!!window.EventSource) {
connect();
} else {
status.textContent = "不支持server-sent"
}
},false);
</script>
<span id="status">Connection closed!</span>
<br>
<span id="output"></span>
</body>
</html>
服務(wù)端代碼
//index.js
const http = require('http');
const fs = require('fs');
http.createServer(function (req, res) {
let interval,
fileName,
index = "./index.html";
console.log(req.url);
if(req.url === "/") {
fileName = index;
} else {
fileName = "." + req.url;
}
if (fileName === "./stream") {//如果server sent event則設(shè)置相應(yīng)頭信息
res.writeHead(200, {
"Content-Type" : "text/event-stream",
"Cache-Control" : "no-cache",
"Connection": "keep-alive",
})
res.write("retry: 10000\n");//過(guò)10000秒重試
res.write("data: " + (new Date()) + "\n\n");
interval = setInterval(function () {
res.write("data: " + (new Date()) + "\n\n");
}, 1000);
//監(jiān)聽close事件, 用于停止定時(shí)器
req.connection.addListener("close", function () {
clearInterval(interval);
}, false);
} else if (fileName === index) {
//判斷是否為頁(yè)面請(qǐng)求, 并找到相應(yīng)文件返回頁(yè)面
fs.exists(fileName, function (exists) {
if (exists) {
fs.readFile(fileName, function (error, content) {
if (error) {
res.writeHead(500);
res.end();
} else {
res.writeHead(200, {"Content-Type" : "text/html"});
res.end(content, "utf-8");
}
})
} else {
console.log(123);
res.writeHead(404);
res.end();
}
})
} else {
res.writeHead(404);
res.end();
}
}).listen(8080, "127.0.0.1");
console.log("at 8080");
相信看了這些案例你已經(jīng)掌握了方法,更多精彩請(qǐng)關(guān)注php中文網(wǎng)其它相關(guān)文章!
相關(guān)閱讀:
HTML5標(biāo)簽嵌套規(guī)則的詳細(xì)介紹
瀏覽器兼容HTML5和CSS3的問(wèn)題
html5做剪刀石頭布效果的教程
以上就是服務(wù)端主動(dòng)發(fā)送數(shù)據(jù)回客戶端在H5里的實(shí)現(xiàn)步奏的詳細(xì)內(nèi)容,更多請(qǐng)關(guān)注php中文網(wǎng)其它相關(guān)文章!
網(wǎng)站建設(shè)是一個(gè)廣義的術(shù)語(yǔ),涵蓋了許多不同的技能和學(xué)科中所使用的生產(chǎn)和維護(hù)的網(wǎng)站。