NodeJS事件源套接字挂断

时间:2020-05-21 12:02:33

标签: node.js sockets https http-headers eventsource

在NodeJS服务器上,我试图通过我的app.js文件中的streamtest函数连接到流:

function streamtest() {
    var eventSourceInitDict = {headers: {}};
    const es = new EventSource("https://username:password@localhost:9112/stream", eventSourceInitDict);
    es.on('message', msg => console.log(msg));
    es.on('error', err => console.log(err));
    return es
}
streamtest();

但是当它运行时,我每秒都会通过错误事件得到此消息:

Event { type: 'error', message: 'socket hang up' }
Event { type: 'error', message: 'socket hang up' }
Event { type: 'error', message: 'socket hang up' }
Event { type: 'error', message: 'socket hang up' }
Event { type: 'error', message: 'socket hang up' }

当我从服务器运行此命令时 curl -i http://username:password@localhost:9112/stream流正常工作,我得到了以下标头:

HTTP/1.1 200 OK
X-Powered-By: Express
X-Accel-Buffering: no
Cache-Control: no-cache
Content-Type: text/event-stream; charset=utf-8
Connection: keep-alive
Date: Thu, 21 May 2020 11:49:47 GMT
Transfer-Encoding: chunked

是否需要在NodeJS服务器上添加一些配置以正确处理此流?为什么每秒都要关闭插槽?

0 个答案:

没有答案