我的设置非常简单。
NodeJs
export default class StreamController {
public static newMessage = async (req: Request, res: Response) => {
const { userid } = req.params;
res.writeHead(200, {
"Connection": "keep-alive",
"Content-Type": "text/event-stream",
"Cache-Control": "no-cache",
});
setInterval(async () => {
console.log(await MessagesController.hasNewMessage(userid));
res.write(`${JSON.stringify({ hasUnread: await MessagesController.hasNewMessage(userid) })}`);
res.write("\n\n");
}, 5000);
}
}
反应
constructor() {
super();
const uid = getUserId();
const eventSource = new EventSource(`http://localhost:8080/stream/messages/${uid}`)
eventSource.onmessage = (e) => {
console.log(e)
}
}
我可以看到流已打开,但是服务器端发出了数据却没有从服务器传递任何事件。我在做什么错了?
答案 0 :(得分:4)
您正在服务器端传递data
,但没有以event
传递事件名称。这就是我解决问题(简化复制)的方式:
服务器:
app.get('/stream', (req, res) => {
console.log('request received');
res.writeHead(200, {
"Connection": "keep-alive",
"Content-Type": "text/event-stream",
"Cache-Control": "no-cache",
});
setInterval(async () => {
res.write('event: ping\n'); // added these
res.write(`data: ${JSON.stringify({ hasUnread: true })}`);
res.write("\n\n");
}, 5000);
});
客户:
constructor(props) {
super(props);
const eventSource = new EventSource(`http://localhost:3030/stream`);
eventSource.onopen = e => {
console.log(e);
}
eventSource.onmessage = e => {
console.log('onmessage');
console.log(e);
}
eventSource.addEventListener('ping', e => {
console.log(e);
});
}
ping 事件将每5秒触发一次。
现在,如果要调用eventSource.onmessage
,则应在服务器端将event
命名为消息:
...
setInterval(async () => {
res.write('event: message\n'); // message event
res.write(`data: ${JSON.stringify({ hasUnread: true })}`);
res.write("\n\n");
}, 5000);
...
现在您将在控制台中看到:
onmessage MessageEvent {...,数据:“ {'hasUnread':true}”,...}
请注意,在这种情况下,不会再触发 ping 事件。更多信息here。