Javascript node.js中的SSE在res.end()之前不会触发“消息侦听器”。原因在代码或代码中
请给我一些建议。 客户端代码:Javascript node.js中的SSE在res.end()之前不会触发“消息侦听器”。原因在代码或代码中,javascript,node.js,server-sent-events,antivirus,Javascript,Node.js,Server Sent Events,Antivirus,请给我一些建议。 客户端代码: var source = new EventSource('/notifications/stream'); source.addEventListener('open', function(e) { console.log('SSE connected') }, false); source.addEventListener('message', function(e) { alert('yes')
var source = new EventSource('/notifications/stream');
source.addEventListener('open', function(e) {
console.log('SSE connected')
}, false);
source.addEventListener('message', function(e) {
alert('yes')
console.log('SSE connected')
}, false);
服务器代码
// set timeout as high as possible
req.socket.setTimeout(Infinity);
// send headers for event-stream connection
// see spec for more information
res.writeHead(200, {
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache',
'Connection': 'keep-alive'
});
res.write('\n');
setInterval(function(){
console.log('writing');
res.write("id: " + Date.now() + "\n");
res.write("data: " + '{"message":"hello world"} ' + "\n\n");
}, 1000);
什么都不管用。。。但是,如果我在res.write客户端触发警报一次之后添加res.end,服务器崩溃,因为它已经结束res。我读到我的客户端在连接关闭之前不会触发,所以我关闭了防病毒功能,但它没有帮助。所以我的问题是:
是代码问题还是其他原因
如果是防病毒软件,是客户端防病毒软件还是服务器防病毒软件?我在Windows上开发Nod32防病毒软件,如果我要在linux上部署,它会有帮助吗?还是取决于客户端的防病毒软件
除了websocket,还有其他选择吗?我唯一需要的是以最小的内存使用量推送通知
NodeJS版本0.10.28
谢谢大家的关注。
对不起,英语不好。问题解决了
问题在于我在express中使用的中间件压缩
由于压缩的性质,此模块不能与服务器发送的事件一起开箱即用。要压缩内容,需要缓冲输出窗口以获得良好的压缩。通常,在使用服务器发送的事件时,需要到达客户机的数据块
当需要将数据写入客户端时,可以通过调用res.flush来实现这一点