📅  最后修改于: 2023-12-03 15:17:54.904000             🧑  作者: Mango
Node.js 中的 http2stream.closed()
方法是用于检测 HTTP/2 流是否被关闭的 API。该方法返回一个布尔值,表示流是否已经被关闭。
stream.closed();
返回一个布尔值,表示流是否已经被关闭。
true
:表示流已经关闭。false
:表示流还未关闭。const http2 = require('http2');
const server = http2.createServer();
server.on('stream', (stream, headers) => {
console.log(`Stream with headers ${JSON.stringify(headers)} has been opened`);
stream.on('close', () => {
console.log('Stream has been closed');
});
// 检测流是否已经关闭
const isClosed = stream.closed();
console.log(`Stream is closed: ${isClosed}`);
});
server.listen(8080);
输出:
Stream with headers {"content-type":"text/html"} has been opened
Stream is closed: false
Stream has been closed
http2stream.closed()
方法必须在流的 close
事件回调函数中调用,否则无法获取正确的流状态。