Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/google-chrome/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 服务器发送的事件EventStream不触发";“onmessage”;但Chrome Debug在“中”显示数据;“事件流”;标签_Javascript_Google Chrome_Event Stream - Fatal编程技术网

Javascript 服务器发送的事件EventStream不触发";“onmessage”;但Chrome Debug在“中”显示数据;“事件流”;标签

Javascript 服务器发送的事件EventStream不触发";“onmessage”;但Chrome Debug在“中”显示数据;“事件流”;标签,javascript,google-chrome,event-stream,Javascript,Google Chrome,Event Stream,我将SSE与javascript的EventStream内置逻辑一起使用。让onopen返回成功结果时,onmessage回调不起作用。 奇怪的是,在Chrome的EventStream选项卡中,数据结果按我的预期列出 这是我为EventSource编写的JS代码片段 var eventSource; $(function () { eventSource = new EventSource('get/status/'); eventSource.onopen = fun

我将SSE与javascript的EventStream内置逻辑一起使用。让
onopen
返回成功结果时,
onmessage
回调不起作用。 奇怪的是,在Chrome的EventStream选项卡中,数据结果按我的预期列出

这是我为EventSource编写的JS代码片段

var eventSource;
  $(function () {
    eventSource  = new EventSource('get/status/');

    eventSource.onopen = function () {
      console.log("Sse connection opened");
    };

    eventSource.onerror = function () {
      console.log("error occured");
    };

    eventSource.onmessage = function (event) {
      console.log("received");
    };

  });

正如您所看到的,有数据被发出,但从来不会触发
onmessage
。有人能向我解释一下这种行为吗


EDIT
onopen
onerror
两种方法都可以。根据屏幕截图,您的事件流包含类型为
“foo”
的事件,而不是
“message”
。这只是默认类型(在事件流中缺少
事件
字段)。发件人:

  • 将事件的类型属性初始化为
    消息
    ,[…]

  • 如果事件类型缓冲区的值不是空字符串, 将新创建事件的类型更改为等于 事件类型缓冲区

  • 在这里,您应该将侦听器设置为侦听
    “foo”
    事件:

    eventSource.addEventListener("foo", function() {...})