http2.connect(authority[, options][, listener])
authority
<string> | <URL> 要连接的远程 HTTP/2 服务器。 这必须是带有http://
或https://
前缀、主机名和 IP 端口(如果使用非默认端口)的最小有效 URL 的形式。 URL 中的用户信息(用户 ID 和密码)、路径、查询字符串和片段详细信息将被忽略。options
<Object>-
maxDeflateDynamicTableSize
<number> 设置用于压缩标头字段的最大动态表大小。 默认值:4Kib
。 -
maxSettings
<number> 设置每SETTINGS
帧的最大设置条目数。 允许的最小值为1
。 默认值:32
。 -
maxSessionMemory
<number> 设置Http2Session
允许使用的最大内存。 该值以兆字节数表示,例如1
等于 1 兆字节。 允许的最小值为1
。 这是一个基于信用的限制,现有的Http2Stream
可能会导致超出此限制,但超过此限制时,新的Http2Stream
实例将被拒绝。 当前Http2Stream
会话数、标头压缩表的当前内存使用、当前排队等待发送的数据以及未确认的PING
和SETTINGS
帧都计入当前限制。 默认值:10
。 -
maxHeaderListPairs
<number> 设置标头条目的最大数量。 这类似于http.Server#maxHeadersCount
或http.ClientRequest#maxHeadersCount
。 最小值为1
。 默认值:128
。 -
maxOutstandingPings
<number> 设置未确认的未确认 ping 的最大数量。 默认值:10
。 -
maxReservedRemoteStreams
<number> 设置客户端在任何给定时间将接受的最大保留推送流数。 一旦当前预留的推送流数量超过此限制,则服务器发送的新推送流将被自动拒绝。 最小允许值为 0。 最大允许值为 232-1。 负值将此选项设置为最大允许值。 默认值:200
。 -
maxSendHeaderBlockLength
<number> 设置序列化的、压缩的标头块的最大允许大小。 尝试发送超出此限制的标头将导致触发'frameError'
事件并且流被关闭和销毁。 -
paddingStrategy
<number> 用于确定用于HEADERS
和DATA
帧的填充量的策略。 默认值:http2.constants.PADDING_STRATEGY_NONE
。 值可能是以下之一:-
http2.constants.PADDING_STRATEGY_NONE
: -
http2.constants.PADDING_STRATEGY_MAX
: -
http2.constants.PADDING_STRATEGY_CALLBACK
: -
http2.constants.PADDING_STRATEGY_ALIGNED
:
-
-
peerMaxConcurrentStreams
<number> 设置远程对等方的最大并发流数,就好像已收到SETTINGS
帧一样。 如果远程对等方为maxConcurrentStreams
设置了自己的值,则将被覆盖。 默认值:100
。 -
selectPadding
<Function> -
protocol
<string> 要连接的协议,如果在authority
中没有设置。 值可以是'http:'
或'https:'
。 默认值:'https:'
-
settings
<HTTP/2 Settings Object> 连接时发送到远程对等方的初始设置。 -
createConnection
<Function> 可选的回调,它接收传给connect
和options
对象的URL
实例,并返回将用作此会话连接的任何Duplex
流。 -
...: 可以提供任何
net.connect()
或tls.connect()
选项。 -
unknownProtocolTimeout
<number> 指定在触发'unknownProtocol'
事件时服务器应等待的超时(以毫秒为单位)。 如果到那时套接字还没有被销毁,则服务器将销毁它。 默认值:10000
。
-
listener
<Function> 将注册为'connect'
事件的单次监听器。- 返回: <ClientHttp2Session>
返回 ClientHttp2Session
实例。
const http2 = require('http2');
const client = http2.connect('https://localhost:1234');
/* 使用客户端 */
client.close();
authority
<string> | <URL> The remote HTTP/2 server to connect to. This must be in the form of a minimal, valid URL with thehttp://
orhttps://
prefix, host name, and IP port (if a non-default port is used). Userinfo (user ID and password), path, querystring, and fragment details in the URL will be ignored.options
<Object>maxDeflateDynamicTableSize
<number> Sets the maximum dynamic table size for deflating header fields. Default:4Kib
.maxSettings
<number> Sets the maximum number of settings entries perSETTINGS
frame. The minimum value allowed is1
. Default:32
.maxSessionMemory
<number> Sets the maximum memory that theHttp2Session
is permitted to use. The value is expressed in terms of number of megabytes, e.g.1
equal 1 megabyte. The minimum value allowed is1
. This is a credit based limit, existingHttp2Stream
s may cause this limit to be exceeded, but newHttp2Stream
instances will be rejected while this limit is exceeded. The current number ofHttp2Stream
sessions, the current memory use of the header compression tables, current data queued to be sent, and unacknowledgedPING
andSETTINGS
frames are all counted towards the current limit. Default:10
.maxHeaderListPairs
<number> Sets the maximum number of header entries. This is similar tohttp.Server#maxHeadersCount
orhttp.ClientRequest#maxHeadersCount
. The minimum value is1
. Default:128
.maxOutstandingPings
<number> Sets the maximum number of outstanding, unacknowledged pings. Default:10
.maxReservedRemoteStreams
<number> Sets the maximum number of reserved push streams the client will accept at any given time. Once the current number of currently reserved push streams exceeds reaches this limit, new push streams sent by the server will be automatically rejected. The minimum allowed value is 0. The maximum allowed value is 2232-1. A negative value sets this option to the maximum allowed value. Default:200
.maxSendHeaderBlockLength
<number> Sets the maximum allowed size for a serialized, compressed block of headers. Attempts to send headers that exceed this limit will result in a'frameError'
event being emitted and the stream being closed and destroyed.paddingStrategy
<number> Strategy used for determining the amount of padding to use forHEADERS
andDATA
frames. Default:http2.constants.PADDING_STRATEGY_NONE
. Value may be one of:http2.constants.PADDING_STRATEGY_NONE
: Specifies that no padding is to be applied.http2.constants.PADDING_STRATEGY_MAX
: Specifies that the maximum amount of padding, as determined by the internal implementation, is to be applied.http2.constants.PADDING_STRATEGY_CALLBACK
: Specifies that the user providedoptions.selectPadding()
callback is to be used to determine the amount of padding.http2.constants.PADDING_STRATEGY_ALIGNED
: Will attempt to apply enough padding to ensure that the total frame length, including the 9-byte header, is a multiple of 8. For each frame, however, there is a maximum allowed number of padding bytes that is determined by current flow control state and settings. If this maximum is less than the calculated amount needed to ensure alignment, the maximum will be used and the total frame length will not necessarily be aligned at 8 bytes.
peerMaxConcurrentStreams
<number> Sets the maximum number of concurrent streams for the remote peer as if aSETTINGS
frame had been received. Will be overridden if the remote peer sets its own value formaxConcurrentStreams
. Default:100
.selectPadding
<Function> Whenoptions.paddingStrategy
is equal tohttp2.constants.PADDING_STRATEGY_CALLBACK
, provides the callback function used to determine the padding. See Usingoptions.selectPadding()
.protocol
<string> The protocol to connect with, if not set in theauthority
. Value may be either'http:'
or'https:'
. Default:'https:'
settings
<HTTP/2 Settings Object> The initial settings to send to the remote peer upon connection.createConnection
<Function> An optional callback that receives theURL
instance passed toconnect
and theoptions
object, and returns anyDuplex
stream that is to be used as the connection for this session.- ...: Any
net.connect()
ortls.connect()
options can be provided. unknownProtocolTimeout
<number> Specifies a timeout in milliseconds that a server should wait when an'unknownProtocol'
event is emitted. If the socket has not been destroyed by that time the server will destroy it. Default:10000
.
listener
<Function> Will be registered as a one-time listener of the'connect'
event.- Returns: <ClientHttp2Session>
Returns a ClientHttp2Session
instance.
const http2 = require('http2');
const client = http2.connect('https://localhost:1234');
/* Use the client */
client.close();