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

前端 未结 8 1543
野趣味
野趣味 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);

提交回复
热议问题