socket.connect(options[, connectListener])
-
options
<Object> -
connectListener
<Function>socket.connect()
方法的常用参数。将被添加为'connect'
事件的监听器一次。¥
connectListener
<Function> Common parameter ofsocket.connect()
methods. Will be added as a listener for the'connect'
event once. -
返回:<net.Socket> 套接字自身
¥Returns: <net.Socket> The socket itself.
在给定套接字上启动连接。通常不需要此方法,应使用 net.createConnection()
创建并打开套接字。仅在实现自定义套接字时使用它。
¥Initiate a connection on a given socket. Normally this method is not needed,
the socket should be created and opened with net.createConnection()
. Use
this only when implementing a custom Socket.
对于 TCP 连接,可用的 options
是:
¥For TCP connections, available options
are:
-
autoSelectFamily
<boolean> :如果设置为true
,它会启用一个家庭自动检测算法,该算法松散地实现了 RFC 8305 的第 5 节。传递给 lookup 的all
选项设置为true
,套接字会依次尝试连接到所有获得的 IPv6 和 IPv4 地址,直到建立连接。首先尝试返回的第一个 AAAA 地址,然后是第一个返回的 A 地址,然后是第二个返回的 AAAA 地址,依此类推。每次连接尝试(但最后一次)都会在超时并尝试下一个地址之前获得autoSelectFamilyAttemptTimeout
选项指定的时间量。如果family
选项不是0
或设置了localAddress
,则忽略。如果至少有一个连接成功,则不会触发连接错误。如果所有连接尝试都失败,则会触发包含所有失败尝试的单个AggregateError
。默认值:net.getDefaultAutoSelectFamily()
。¥
autoSelectFamily
<boolean>: If set totrue
, it enables a family autodetection algorithm that loosely implements section 5 of RFC 8305. Theall
option passed to lookup is set totrue
and the sockets attempts to connect to all obtained IPv6 and IPv4 addresses, in sequence, until a connection is established. The first returned AAAA address is tried first, then the first returned A address, then the second returned AAAA address and so on. Each connection attempt (but the last one) is given the amount of time specified by theautoSelectFamilyAttemptTimeout
option before timing out and trying the next address. Ignored if thefamily
option is not0
or iflocalAddress
is set. Connection errors are not emitted if at least one connection succeeds. If all connections attempts fails, a singleAggregateError
with all failed attempts is emitted. Default:net.getDefaultAutoSelectFamily()
. -
autoSelectFamilyAttemptTimeout
<number> :使用autoSelectFamily
选项时在尝试下一个地址之前等待连接尝试完成的时间量(以毫秒为单位)。如果设置为小于10
的正整数,则将使用值10
。默认值:net.getDefaultAutoSelectFamilyAttemptTimeout()
。¥
autoSelectFamilyAttemptTimeout
<number>: The amount of time in milliseconds to wait for a connection attempt to finish before trying the next address when using theautoSelectFamily
option. If set to a positive integer less than10
, then the value10
will be used instead. Default:net.getDefaultAutoSelectFamilyAttemptTimeout()
. -
family
<number> :IP 栈的版本。必须是4
、6
或0
。值0
表示允许使用 IPv4 和 IPv6 地址。默认值:0
。¥
family
<number>: Version of IP stack. Must be4
,6
, or0
. The value0
indicates that both IPv4 and IPv6 addresses are allowed. Default:0
. -
hints
<number> 可选dns.lookup()
提示。¥
hints
<number> Optionaldns.lookup()
hints. -
host
<string> 套接字应连接到的主机。默认值:'localhost'
。¥
host
<string> Host the socket should connect to. Default:'localhost'
. -
keepAlive
<boolean> 如果设置为true
,则它会在连接建立后立即在套接字上启用保持活动功能,类似于在socket.setKeepAlive()
中所做的。默认值:false
。¥
keepAlive
<boolean> If set totrue
, it enables keep-alive functionality on the socket immediately after the connection is established, similarly on what is done insocket.setKeepAlive()
. Default:false
. -
keepAliveInitialDelay
<number> 如果设置为正数,则它会设置在空闲套接字上发送第一个保持活跃探测之前的初始延迟。默认值:0
。¥
keepAliveInitialDelay
<number> If set to a positive number, it sets the initial delay before the first keepalive probe is sent on an idle socket. Default:0
. -
localAddress
<string> 套接字应该连接的本地地址。¥
localAddress
<string> Local address the socket should connect from. -
localPort
<number> 套接字应连接的本地端口。¥
localPort
<number> Local port the socket should connect from. -
lookup
<Function> 自定义查找函数。默认值:dns.lookup()
。¥
lookup
<Function> Custom lookup function. Default:dns.lookup()
. -
noDelay
<boolean> 如果设置为true
,则它会在套接字建立后立即禁用 Nagle 算法。默认值:false
。¥
noDelay
<boolean> If set totrue
, it disables the use of Nagle's algorithm immediately after the socket is established. Default:false
. -
port
<number> 必需的。套接字应连接到的端口。¥
port
<number> Required. Port the socket should connect to. -
blockList
<net.BlockList>blockList
可用于禁用对特定 IP 地址、IP 范围或 IP 子网的出站访问。¥
blockList
<net.BlockList>blockList
can be used for disabling outbound access to specific IP addresses, IP ranges, or IP subnets.
对于 IPC 连接,可用的 options
是:
¥For IPC connections, available options
are:
-
path
<string> 必需的。客户端应该连接的路径。参见 识别 IPC 连接的路径。如果提供,上面的 TCP 特定选项将被忽略。¥
path
<string> Required. Path the client should connect to. See Identifying paths for IPC connections. If provided, the TCP-specific options above are ignored.