http2stream.pushStream(headers[, options], callback)
headers
<HTTP/2 Headers Object>options
<Object>callback
<Function> 推送流启动后调用的回调。err
<Error>pushStream
<ServerHttp2Stream> 返回的pushStream
对象。headers
<HTTP/2 Headers Object> 用于启动pushStream
的标头对象。
启动推送流。
使用为作为第二个参数传入的推送流创建的新 Http2Stream
实例或作为第一个参数传入的 Error
调用回调。
const http2 = require('node:http2');
const server = http2.createServer();
server.on('stream', (stream) => {
stream.respond({ ':status': 200 });
stream.pushStream({ ':path': '/' }, (err, pushStream, headers) => {
if (err) throw err;
pushStream.respond({ ':status': 200 });
pushStream.end('some pushed data');
});
stream.end('some data');
});
HEADERS
帧中不允许设置推流的权重。
将 weight
值传给 http2stream.priority
,并将 silent
选项设置为 true
,以启用并发流之间的服务器端带宽平衡。
不允许从推送的流中调用 http2stream.pushStream()
并且会抛出错误。
headers
<HTTP/2 Headers Object>options
<Object>exclusive
<boolean> Whentrue
andparent
identifies a parent Stream, the created stream is made the sole direct dependency of the parent, with all other existing dependents made a dependent of the newly created stream. Default:false
.parent
<number> Specifies the numeric identifier of a stream the newly created stream is dependent on.
callback
<Function> Callback that is called once the push stream has been initiated.err
<Error>pushStream
<ServerHttp2Stream> The returnedpushStream
object.headers
<HTTP/2 Headers Object> Headers object thepushStream
was initiated with.
Initiates a push stream. The callback is invoked with the new Http2Stream
instance created for the push stream passed as the second argument, or an
Error
passed as the first argument.
const http2 = require('node:http2');
const server = http2.createServer();
server.on('stream', (stream) => {
stream.respond({ ':status': 200 });
stream.pushStream({ ':path': '/' }, (err, pushStream, headers) => {
if (err) throw err;
pushStream.respond({ ':status': 200 });
pushStream.end('some pushed data');
});
stream.end('some data');
});
Setting the weight of a push stream is not allowed in the HEADERS
frame. Pass
a weight
value to http2stream.priority
with the silent
option set to
true
to enable server-side bandwidth balancing between concurrent streams.
Calling http2stream.pushStream()
from within a pushed stream is not permitted
and will throw an error.