简体   繁体   中英

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**
    };

There are situations in the communication between the browser and the client where the server is pushing data as I see in the server log but the client doesn't display it. See a snapshot below

127.0.0.1 - - [06/Mar/2014 21:31:23] "GET /updates/1 HTTP/1.1" 200 - 60.2671

As you can see the URL /updates/1 is the EventSource(url) that I use and 60.2671 is the seconds. Is this bad that this is such a long request or is it supposed to be that way.

This problem is solved I modified my code to add the line

EventMachine::PeriodicTimer.new(20) { out << "data: \n\n" } # required, otherwise the connection is closed in 30-60 sec

Adding this line ensures that the persistent connection that you keep_open stays open for as long as possible. Otherwise it times out and then reconnects causing events being missed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM