http2stream.pushStream(headers[, options], callback)


启动推送流。 使用为作为第二个参数传入的推送流创建的新 Http2Stream 实例或作为第一个参数传入的 Error 调用回调。

const http2 = require('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> When true and parent 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.

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('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.