13

我使用 EventSource 在我的 JavaScript 客户端应用程序中有推送通知。我可以像这样附加事件侦听器:

source.addEventListener('my_custom_event_type', function(e) {
  console.log(e.data);
}, false);

但是我想监视从服务器推送的所有事件(基本上是为了调试),所以如果发送了一些事件但它没有事件监听器,我可以很容易地找到它。我的意思是,我不想只是“忽略”所有没有绑定 eventListeners 的事件。

我希望做这样的事情:

source.addEventListener('*', function(e) {
  console.debug('Event with no listener attached: ', e);
}, false);

但是html5rocks上的规范和教程没有说明这是否可能。

另一方面,它可能是一些允许监视所有服务器事件或其他东西的 firefox/chrome 扩展。这些东西真的有助于开发推送通知。

谢谢!

4

5 回答 5

29

我自己想出了一个解决方案,它也极大地改进了 EventSource 接口。

服务器端:不发送事件类型,只包含一个额外的数据字段(我总是使用 json)。所以而不是

event: eventName
data: {mykey: 'myvalue'}

我改为从服务器发送这个:

data: {mykey: 'myvalue', eventName: 'eventName'}

客户端:现在我可以使用 EventSource onmessage 回调,它会在每条没有事件类型的消息上触发。

对于绑定事件侦听器,我创建了一个具有 Backbone.Event 功能的包装类。结果:

// Server Sent Events (Event Source wrapper class)
var MyEventSource = (function() {

  function MyEventSource(url) {
    var self = this;
    _.extend(this, Backbone.Events);

    this.source = new EventSource(url);
    this.source.onmessage = function(event) {
      var data, eventName;
      var data = JSON.parse(event.data);
      var eventName = data.eventName; delete data.eventName;

      // Now we can monitor all server sent events
      console.log('app.server.on ', eventName, '. Data: ', data);

      self.trigger(eventName, data);
    };
  }

  return MyEventSource;
})();

现在有了这个包装类,我可以轻松扩展功能,可以轻松监控所有服务器发送的事件,并且由于扩展 Backbone.Events,此类中的事件处理功能更加强大。

使用示例:

var source = new MyEventSource('url/of/source');

// Add event listener
source.on('eventName', function(data) {
  console.log(data);
});

// Fire a event (also very useful for testing and debugging!!)
source.trigger('eventName', { mykey: 'myvalue' });

// Unbind event listener (very important for complex applications)
source.off('eventName');

现在我有了一个易于处理、扩展、调试和测试的组件。

于 2012-03-30T03:50:49.880 回答
0
 <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js" type="text/javascript"></script>  
  <script>
    var content = '';
    if(typeof(EventSource)!=="undefined")
    {
      var source = new EventSource("demo_sse.php");
      source.onmessage = function(event)
      {
        content+=event.data + "<br>";
        $("#result").html(content);
      };
    }
    else
    {
      $("#result").html("Sorry, your browser does not support server-sent events...");
    }
  </script>
于 2014-05-03T01:08:51.683 回答
0

感谢上面的用户tothemario提供我需要弄清楚的线索。

看来您可以使用自定义类型将事件发送回浏览器,但为了触发 MESSAGE 事件,您必须将侦听器分配给新类型而不是message类型。

如果您查看下面的客户端代码,它有望说明。

对于上下文,我的服务器发送一个自定义类型的事件CustomType。因此,我订阅了该类型的事件侦听器,并添加了另一个侦听器message作为其他所有内容的全部捕获。

在此工作流程中,带有CustomType不同侦听器的浏览器会触发一个事件。

 <script type="text/javascript">
    var CustomTypeList = [];

    function EventSystemOpen(e) {
        console.log("EventSystemOpen", e);
    }

    function EventSystemError(e) {
        console.log("EventSystemOpen", e);
        if (e.readyState == EventSource.CLOSED) {
            //
        }
    }

    function GotServerEventMessage(e) {
        console.log("GotServerEventMessage", e);
    }

    function GotCustomType(e) {
        CustomTypeList.push(JSON.parse(e.data));
        console.log("Added CustomType", e, JSON.parse(e.data), CustomTypeList);
    }

    if (!!window.EventSource) {
        var source = new EventSource('api/listen');
        source.addEventListener('open', EventSystemOpen, false);
        source.addEventListener('error', EventSystemError, false);
        source.addEventListener('message', GotServerEventMessage, false);
        source.addEventListener('CustomType', GotCustomType, false);
    }
 </script>
于 2019-05-24T19:02:15.420 回答
0

您可以onmessage像这样简单地使用用户。

const evtSource = new EventSource("ssedemo.php");
evtSource.onmessage = function(event) {
   console.log("event " + event.data);
}
于 2021-09-17T13:58:04.077 回答
0

我知道这不是 EventSource,但我一直在寻找相同的东西(一种在不知道它们类型的情况下捕获所有传入事件的方法)。由于无法控制发送这些事件的服务器,我最终只使用 XHR 编写它,以防其他人遇到这种情况:

function eventStream(path, callback){
    //Create XHR object
    var xhr = new XMLHttpRequest();

    //initialize storage for previously fetched information
    var fetched='';

    //Set readystatechange handler
    xhr.onreadystatechange=function(){

        //If the connection has been made and we have 200, process the data
        if(xhr.readyState>2 && xhr.status==200){
            //save the current response text
            var newFetched=xhr.responseText;

            //this is a stream, so responseText always contains everything
            //from the start of the stream, we only want the latest
            var lastFetch=xhr.responseText.replace(fetched, '');

            //Set the complete response text to be removed next time 
            var fetched=newFetched;

            //callback to allow parsing of the fetched data
            callback(lastFetch);
        }
    };

    //open and send to begin the stream;
    xhr.open('GET', path, true);
    xhr.send();
}

parseEvents=function(response){
    var events=[];
    //split out by line break
    var lines=response.split("\n");

    //loop through the lines
    for(var i=0;i<lines.length;i++){

        //each event consists of 2 lines, one begins with
        //"name:", the other with "data"
        //if we hit data, process it and the previous line
        if(lines[i].substr(0, lines[i].indexOf(':'))=='data'){

            //add this event to our list for return
            events.push({

               //get the event name
               name: lines[i-1].split(':')[1].trim(),
               //parse the event data
               data: $.parseJSON(lines[i].substr(lines[i].indexOf(':')+1).trim())
            });
        }
    }
    //return the parsed events
    return events;
};

evenStream('http://example.com/myEventPath', function(response){
    var events=parseEvents(response);
});
于 2015-12-22T14:58:21.797 回答