问题
I'm using SSE with the EventStream built in logic for javascript. While having onopen
return a successful result the onmessage
callback is not working.
Odd thing here is that within Chrome in the EventStream tab the data results are listed as I expect them.
This is my JS snippet for the EventSource
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");
};
});
As you can see there is data being emitted but onmessage
is never triggered. Can anybody explain this behaviour to me?
EDIT: onopen
and onerror
do both work.
回答1:
According to the screenshot, your event-stream contains events of type "foo"
, not "message"
. That is only the default type (absent an event
field in the event-stream). From the spec:
Initialize event's type attribute to
message
, […]If the event type buffer has a value other than the empty string, change the type of the newly created event to equal the value of the event type buffer.
Here, you should set your listener to listen for "foo"
events:
eventSource.addEventListener("foo", function() {...})
来源:https://stackoverflow.com/questions/46971499/server-sent-event-eventstream-does-not-trigger-onmessage-but-chrome-debug-show