服务器发送事件生成错误 ERR_INCOMPLETE_CHUNKED_ENCODING

时间:2021-04-18 10:52:05

标签: javascript node.js typescript express server-sent-events

我有 ExpressJS 服务器(在 NodeJS 中),它使用服务器发送事件来宣布服务器数据库的更改。

我的 NodeJS 应用程序中的实际处理程序如下所示:

this._app.use((req, res, next) => {
    if (req.url.includes("addDBListener")) {
        res.writeHead(200, {
            'Content-Type': 'text/event-stream',
            'Cache-Control': 'no-cache',
            'Connection': 'keep-alive'
        });
        res.write('\n\n');
        let path = (req.query && req.query.path) ? req.query.path : "";
        this.dbListeners.push({
           path: path,
           res: res
        });
    }
    else {
        next();
    }
});

然后,当数据库发生任何变化时,我会以这种方式向客户端发送信息(“i”是客户端的索引,我向其发送新信息):

this.clientDBListeners[i].res.write("data: " + JSON.stringify(DBPart) + "\n\n");

在客户端,我有这个来初始化(和处理)事件处理:

source = new EventSource('/addDBListener?path=' + dbPath)

let messageHandler = (e)=> {
    console.log("DATAA: "+e.data);
}

let errorHandler = (e)=> { 
    //process error
    console.log("ERROR!!!");
}

source.addEventListener('message', messageHandler);
source.addEventListener('error',  errorHandler)

整个代码都可以工作,我从服务器正确获取更新等等......但一件奇怪的事情是,我每大约 2.5 分钟就会在 Google chrome 控制台中收到“奇怪”的错误(所以一小时后控制台充满了错误)。 . 我真的不知道为什么。

错误如下:
无法加载资源:net::ERR_INCOMPLETE_CHUNKED_ENCODING

GET http://192.168.1.4/addDBListener?path=/rooms/ net::ERR_INCOMPLETE_CHUNKED_ENCODING 200 (OK)

第一个在客户端启动时显示一次,第二个重复发生,见图:
enter image description here

我从服务器发送附加了“\n\n”和 Connection: keep-alive 的消息,所以我真的不知道,问题出在哪里...

谢谢!

0 个答案:

没有答案