1
請諮詢someting。 客戶端代碼:node.js中的SSE在res.end()之前不會觸發「消息偵聽器」。原因是在代碼或
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);
Server代碼
// 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.end()
後res.write()
客戶火災alert()
一倍,服務器崩潰,因爲它已經結束了res
。我讀that it might be antivirus's problem,我的客戶端在連接關閉之前不會觸發,所以我關閉了防病毒功能,但沒有幫助。所以我的問題是:
- 它是代碼的問題或其他原因(殺毒軟件)
- 如果它是由病毒,它是客戶端防病毒軟件或服務器的防病毒的問題? (我正在使用Nod32殺毒軟件在Windows上開發,如果我將在Linux上部署,它會有所幫助,還是取決於客戶端的防病毒程序)
- 除了websocket以外,還有其他選擇嗎(我只需要推送內存最少的通知)
NodeJS版本0.10.28 感謝您的關注。 抱歉,糟糕的英語。