[英]How to reduce latency and not miss events from server in Server Side Events
es.onmessage = function(e) {
var newElement = document.createElement("li");
newElement.innerHTML = e.data;
eventList.appendChild(newElement);
};
es.onerror = function(e) {
**//what to add here to ensure I don't miss events sent during this time frame, or state**
};
在瀏覽器和客戶端之間的通信中,有些情況下服務器正在推送數據,如我在服務器日志中看到的那樣,但客戶端未顯示數據。 查看下面的快照
127.0.0.1 - - [06/Mar/2014 21:31:23] "GET /updates/1 HTTP/1.1" 200 - 60.2671
如您所見,URL / updates / 1是我使用的EventSource(url),而60.2671是秒。 這是一個很長的請求是否很糟糕,還是應該那樣呢?
解決了這個問題,我修改了代碼以添加行
EventMachine::PeriodicTimer.new(20) { out << "data: \n\n" } # required, otherwise the connection is closed in 30-60 sec
添加此行可確保keep_open的持久連接保持打開狀態的時間盡可能長。 否則,它會超時,然后重新連接,從而導致事件丟失。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.