如何使用 Express 在 node.js 中关闭 HTTP 连接

How to close HTTP connection in node.js using Express

我对服务器发送的事件使用 node.js (Express)。我想通过关闭 sse HTTP 连接来关闭事件流。给出以下函数:

router.get('/sse', function (req, res) {

});

如何实现?

使用这个

res.end();

它会关闭连接

选择的答案不正确,res.end() 结束响应并写入套接字。但它不会关闭 TCP 连接。

您需要从 res 对象获取底层连接并调用 end 来关闭它。

res.connection.end();

如何在ExpressJS中挂断

Ⅰ.结束响应,保持套接字连接。

response.end();

Ⅱ. “你能先挂电话吗?”

请求客户端发起TCP套接字连接终止序列。

response.set("Connection", "close"); // Note: this is an HTTP header.

Ⅲ。轻轻关闭套接字连接。

“嘿,我挂了,好吗?”

response.connection.end();

socket.end([data[, encoding]][, callback])

Half-closes the socket. i.e., it sends a FIN packet. It is possible the server will still send some data.

See writable.end() for further details.

Ⅳ.只是主动挂断客户端。甚至没有 FIN 数据包。

response.connection.destroy();

socket.destroy([error])

Ensures that no more I/O activity happens on this socket. Destroys the stream and closes the connection.

See writable.destroy() for further details.