Javascript 服务器发送的事件EventStream不触发";“onmessage”;但Chrome Debug在“中”显示数据;“事件流”;标签
我将SSE与javascript的EventStream内置逻辑一起使用。让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
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() {...})