HTML5 Server-Sent Events prototyping - ambiguous error and repeated polling?

前端 未结 8 1488
野趣味
野趣味 2020-12-03 14:44

I\'m trying to get to grips with Server-Side Events as they fit my requirements perfectly and seem like they should be simple to implement, however I can\'t get past a vague

相关标签:
8条回答
  • 2020-12-03 15:26

    I was able to do it by implementing a custom event loop. It seems that this html5 feature is not ready at all and has compatibility issues even with the latest version of google chrome. Here it is, working on firefox (can't get the message sent correctly on chrome) :

    var source;
    
    function Body_Load(event) {
        loopEvent();
    }
    
    function loopEvent() {
        if (source == undefined) {
            source = new EventSource("event/message.php");
        }
        source.onmessage = function(event) {
            _e("out").value = event.data;
            loopEvent();
        }
    }
    

    P.S. : _e is a function that calls document.getElementById(id);

    0 讨论(0)
  • 2020-12-03 15:29

    Server Sent Events are easy only when it comes to the Javascript part. First of all a lot of tutorials on SSE in the internet are closing their connections in the server part. Be it PHP or Java examples. This is really astonishing because what you get then is just a different way of implementing a "Ajax Polling" system with a strictly defined payload structure (and some minor features like client retry values set by server side). You can easily implement that with a few lines of jQuery. No need for SSE then.

    According to the spec of SSE, i would say that the retry shouldnt be the normal way of implementing a client side loop. For me SSE is a one way streaming method which relies on a server backend which does not close the connection after pushing the first data to the client.

    In Java its useful to use Servlet3 Async spec in order to free the request thread immediately and do the processing / streaming in a different thread. This works so far but still i dont like the 30 seconds connection lifetime for the EventSource request. Even i am pushing data every 5 seconds, the connection will be terminated after 30 seconds (chrome, firefox). Of course SSE will reconnect per default after 3 seconds but still i dont think this is the way it should be.

    One problem is that some Java MVC frameworks dont have the ability to keep the connection open after data sending, so that you end up coding to the bare Servlet API. After on 24hours on coding prototypes in Java, i am more or less dissapointed because the gain over a traditional jQuery-Ajax-loop is not THAT much. And the problem with polyfilling the SSE feature is also existant.

    0 讨论(0)
提交回复
热议问题