服务器端事件 - onmessage 未触发
Server Side Event - onmessage not firing
我正在尝试在 javascript 中使用 sse 响应。 onopen 和 onerror 工作正常但 onmessage 不行。
var conn = new EventSource(`${config.serverHost}/log/stream/${this.streamId}`)
conn.onopen = (evt) => {
console.log('connected to ' + logSourceId)
}
conn.onmessage = (evt) => {
console.log(evt)
}
conn.onerror = (evt) => {
console.error(evt)
}
连接建立成功,收到的事件可以在Chrome网络记录中找到,如下图所示。但是永远不会触发 onmessage!
如有任何意见,我们将不胜感激。
您似乎在使用命名事件(类似于 “log streami...”)。
要支持这些,您需要使用 addEventListener
格式。
// assuming the event name is "log streaming"
conn.addEventListener("log streaming", e => {
console.log("log streaming", e)
})
仅供参考,onmessage
适用于任何未命名的事件,也就是那些没有 event
属性.
的 event streams
event
A string identifying the type of event described. If this is specified, an event will be dispatched on the browser to the listener for the specified event name; the website source code should use addEventListener()
to listen for named events. The onmessage
handler is called if no event name is specified for a message.
我正在尝试在 javascript 中使用 sse 响应。 onopen 和 onerror 工作正常但 onmessage 不行。
var conn = new EventSource(`${config.serverHost}/log/stream/${this.streamId}`)
conn.onopen = (evt) => {
console.log('connected to ' + logSourceId)
}
conn.onmessage = (evt) => {
console.log(evt)
}
conn.onerror = (evt) => {
console.error(evt)
}
连接建立成功,收到的事件可以在Chrome网络记录中找到,如下图所示。但是永远不会触发 onmessage!
如有任何意见,我们将不胜感激。
您似乎在使用命名事件(类似于 “log streami...”)。
要支持这些,您需要使用 addEventListener
格式。
// assuming the event name is "log streaming"
conn.addEventListener("log streaming", e => {
console.log("log streaming", e)
})
仅供参考,onmessage
适用于任何未命名的事件,也就是那些没有 event
属性.
event
A string identifying the type of event described. If this is specified, an event will be dispatched on the browser to the listener for the specified event name; the website source code should use
addEventListener()
to listen for named events. Theonmessage
handler is called if no event name is specified for a message.