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


  • headers <HTTP/2 Headers Object>

  • options <Object>

    • exclusive <boolean>trueparent 标识父流时,创建的流将成为父流的唯一直接依赖,所有其他现有依赖都成为新创建流的依赖。默认值:false

      ¥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> 指定新创建的流所依赖的流的数字标识符。

      ¥parent <number> Specifies the numeric identifier of a stream the newly created stream is dependent on.

  • callback <Function> 推送流启动后调用的回调。

    ¥callback <Function> Callback that is called once the push stream has been initiated.

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

¥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.

import { createServer } from 'node:http2';
const server = 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');
});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,以启用并发流之间的服务器端带宽平衡。

¥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.

不允许从推送的流中调用 http2stream.pushStream() 并且会抛出错误。

¥Calling http2stream.pushStream() from within a pushed stream is not permitted and will throw an error.