Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 获取SSE消息、角度前端、节点后端时出错_Node.js_Angular_Server Sent Events_Eventsource - Fatal编程技术网

Node.js 获取SSE消息、角度前端、节点后端时出错

Node.js 获取SSE消息、角度前端、节点后端时出错,node.js,angular,server-sent-events,eventsource,Node.js,Angular,Server Sent Events,Eventsource,我使用SSE从节点后端向Angular应用程序发送简单消息。到目前为止,它一直在完美地工作,但今天我意识到它不再工作了,我找不到原因 以下是相关代码: 节点 router.get('/stream', function (req, res) { [ ... ] // Send headers for event-stream connection res.writeHead(200, { 'Content-Type': 'text/event-strea

我使用SSE从节点后端向Angular应用程序发送简单消息。到目前为止,它一直在完美地工作,但今天我意识到它不再工作了,我找不到原因

以下是相关代码:

节点

router.get('/stream', function (req, res) {
    [ ... ]

    // Send headers for event-stream connection
    res.writeHead(200, {
        'Content-Type': 'text/event-stream',
        'Cache-Control': 'no-cache',
        'Connection': 'keep-alive'
    });
    res.write('\n');

    [ ... ]
    // NOTE: This line connects to a Redis server
    subscriber.subscribe(channel);

    // In case we encounter an error...print it out to the console
    subscriber.on("error", function (err) {
        logger.debug("Redis Error: " + err);
    });

    // When we receive a message from the Redis connection
    subscriber.on("message", function (channel, message) {
        logger.debug('MESSAGE RECEIVED: ' + message);   // This message is printed in the log file

        [ ... ]
        // Note: 'msgData' is obtained by parsing the received 'message'
        // Send data to the client
        res.write('data: ' + msgData + "\n\n");
    });
}
角度应用程序

declare var EventSource : any;

getSSEMessages(): Observable<string> {
    let sseUrl = `${ConfigService.settings.appURL}/stream`;    

    return new Observable<string>(obs => {
        const es = new EventSource(sseUrl, { withCredentials: true });  

        // This prints '1' (CONNECTION OPEN)
        console.log('SSE Connection: ', es.readyState);               

        // Note: I've also tried 'es.onerror'
        es.addEventListener("error", (error) => {
            console.log("Error in SSE connection", error);

            return false;
        });

        // Note: I've also tried 'es.onmessage'
        es.addEventListener("message", (evt : any) => {
            // Never gets in here
            console.log('SSE Message', evt.data);

            obs.next(evt.data);
        });

        return () => es.close();
    });
}
正如我所说,几周来一切都很顺利。今天我看到我没有收到任何消息,我一直在努力寻找原因,但运气不好


有什么线索可以说明问题吗?

最后,我找到了一个解决方案:

可能是因为更新了一些节点模块或节点本身,它添加了某种缓冲区来优化连接流量。要使应用程序再次工作,我所要做的就是在每条消息后添加一个
flush
,如下所示:

[...]
res.write('data: ' + msgData + "\n\n");

// Send the message instantly
res.flush();
[...]

干杯,

使用Chrome开发工具的网络选项卡,我刚刚意识到,当我请求流路由时,会创建一个新的“eventsource”项,“Time”列显示“Pending”,与所有其他请求不同,它以毫秒为单位显示时间。这与问题有关吗?谢谢。有点像这个问题。。您不需要删除“/stream”处理程序中的事件侦听器吗?我觉得上面的代码会导致内存泄漏!我们从SSE/Redis切换到Socket.IO,所以我无法回答您的问题,抱歉!
[...]
res.write('data: ' + msgData + "\n\n");

// Send the message instantly
res.flush();
[...]