這可能不是很有幫助,但我所聽到的最好的是Comet streams。這是一種較老的方法,很多人不喜歡它(包括我自己),但它是單向服務器到客戶端更新的選項。
從本質上講,在客戶端你有連接到服務器的iframe
,服務器發回的響應multipart
的形式迴應,偶爾發回script
標籤用的填充位來執行。所以,一個簡單的(也可能打破)的例子是這樣的:
<!--index.html-->
<html>
<body>
<iframe src="/comet/status"></iframe>
</body>
</html>
,然後服務器代碼...
// server code (I like Node.JS)
app.get('/comet/status', function (req, res) {
// A function that does a lot of work,
// and occasionally calls a callback with progress
doWork(function (progress) {
res.write('<script>console.log("Progress: " + progress);</script>');
});
res.end();
});
就像我說的,這是一個相當不完整的例子,但它是一個即使以更老的方式完成你要找的東西。而不是控制檯日誌記錄,你可能會更新顯示進度的元素。
[服務器發送的事件]怎麼樣(http://stackoverflow.com/questions/5195452/websockets-vs-server-sent-events-eventsource) – laggingreflex 2015-04-04 14:19:35
看到這個SO問題:http://stackoverflow.com/問題/ 21057882 /主要差異 - 雙向插座和方向插座 – MvdD 2015-04-04 17:30:33
SSE認爲是安全的嗎?它是否防止拒絕服務? – Robin 2015-04-04 19:12:08