Http2Stream 类


Http2Stream 类的每个实例代表一个通过 Http2Session 实例的双向 HTTP/2 通信流。 任何单个 Http2Session 在其生命周期中最多可能有 231-1 个 Http2Stream 实例。

用户代码不会直接构造 Http2Stream 实例。 而是,这些是通过 Http2Session 实例创建、管理并提供给用户代码的。 在服务器上,创建 Http2Stream 实例是为了响应传入的 HTTP 请求(并通过 'stream' 事件传给用户代码),或者响应对 http2stream.pushStream() 方法的调用。 在客户端,当调用 http2session.request() 方法或响应传入的 'push' 事件时,会创建并返回 Http2Stream 实例。

Http2Stream 类是 ServerHttp2StreamClientHttp2Stream 类的基础,每个类分别由服务器端或客户端专门使用。

所有 Http2Stream 实例都是 Duplex 流。 DuplexWritable 端用于向连接的对端发送数据,而 Readable 端用于接收连接的对端发送的数据。

Http2Stream 的默认文本字符编码为 UTF-8。 当使用 Http2Stream 发送文本时,使用 'content-type' 标头设置字符编码。

stream.respond({
  'content-type': 'text/html; charset=utf-8',
  ':status': 200,
});

Each instance of the Http2Stream class represents a bidirectional HTTP/2 communications stream over an Http2Session instance. Any single Http2Session may have up to 231-1 Http2Stream instances over its lifetime.

User code will not construct Http2Stream instances directly. Rather, these are created, managed, and provided to user code through the Http2Session instance. On the server, Http2Stream instances are created either in response to an incoming HTTP request (and handed off to user code via the 'stream' event), or in response to a call to the http2stream.pushStream() method. On the client, Http2Stream instances are created and returned when either the http2session.request() method is called, or in response to an incoming 'push' event.

The Http2Stream class is a base for the ServerHttp2Stream and ClientHttp2Stream classes, each of which is used specifically by either the Server or Client side, respectively.

All Http2Stream instances are Duplex streams. The Writable side of the Duplex is used to send data to the connected peer, while the Readable side is used to receive data sent by the connected peer.

The default text character encoding for an Http2Stream is UTF-8. When using an Http2Stream to send text, use the 'content-type' header to set the character encoding.

stream.respond({
  'content-type': 'text/html; charset=utf-8',
  ':status': 200,
});