2 回答

TA贡献1831条经验 获得超9个赞
显然,EventSource只能使用其文档中定义的特定格式。正如您在此示例中看到的,消息数据应以某种data: [YOUR_DATA]\n\n格式发送。
你应该在你的服务器端使用这样的东西:
app.get('/event-stream', (req, res) => {
res.status(200).set({
"connection": "keep-alive",
"cache-control": "no-cache",
"content-Type": "text/event-stream"
});
let data = { name: "Mithoon Kumar" }
const intervalId = setInterval(() => {
res.write(`data: ${JSON.stringify(data)}\n\n`);
}, 1000);
req.on('close', () => {
clearInterval(intervalId);
});
});

TA贡献1810条经验 获得超4个赞
我编辑了服务器正在写入数据的行并开始工作。setInterval(()=>{ response.write( data: Hello world!\n\n
)}, 0)
添加回答
举报