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:
-
port<number> 必需的。套接字应连接到的端口。¥
port<number> Required. Port the socket should connect to. -
host<string> 套接字应连接到的主机。默认值:'localhost'。¥
host<string> Host the socket should connect to. Default:'localhost'. -
localAddress<string> 套接字应该连接的本地地址。¥
localAddress<string> Local address the socket should connect from. -
localPort<number> 套接字应连接的本地端口。¥
localPort<number> Local port the socket should connect from. -
family<number> :IP 栈的版本。必须是4、6或0。值0表示允许使用 IPv4 和 IPv6 地址。默认值:0。¥
family<number>: Version of IP stack. Must be4,6, or0. The value0indicates that both IPv4 and IPv6 addresses are allowed. Default:0. -
hints<number> 可选dns.lookup()提示。¥
hints<number> Optionaldns.lookup()hints. -
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. -
keepAlive<boolean> 如果设置为true,则它会在连接建立后立即在套接字上启用保持活动功能,类似于在socket.setKeepAlive([enable][, initialDelay])中所做的。默认值: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([enable][, initialDelay]). 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.
对于 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.
对于这两种类型,可用的 options 包括:
¥For both types, available options include:
-
onread<Object> 如果指定,传入的数据存储在单个buffer中,并在数据到达套接字时传给提供的callback。这将导致流式传输功能不提供任何数据。套接字将像往常一样触发'error'、'end'和'close'等事件。pause()和resume()之类的方法也将按预期运行。¥
onread<Object> If specified, incoming data is stored in a singlebufferand passed to the suppliedcallbackwhen data arrives on the socket. This will cause the streaming functionality to not provide any data. The socket will emit events like'error','end', and'close'as usual. Methods likepause()andresume()will also behave as expected.-
buffer<Buffer> | <Uint8Array> | <Function> 用于存储传入数据的可重用内存块或返回此类数据的函数。¥
buffer<Buffer> | <Uint8Array> | <Function> Either a reusable chunk of memory to use for storing incoming data or a function that returns such. -
callback<Function> 为每个传入数据块调用此函数。传递给它的两个参数:写入buffer的字节数和对buffer的引用。从此函数返回false以隐式pause()套接字。该函数将在全局上下文中执行。¥
callback<Function> This function is called for every chunk of incoming data. Two arguments are passed to it: the number of bytes written tobufferand a reference tobuffer. Returnfalsefrom this function to implicitlypause()the socket. This function will be executed in the global context.
-
以下是使用 onread 选项的客户端示例:
¥Following is an example of a client using the onread option:
const net = require('node:net');
net.connect({
port: 80,
onread: {
// Reuses a 4KiB Buffer for every read from the socket.
buffer: Buffer.alloc(4 * 1024),
callback: function(nread, buf) {
// Received data is available in `buf` from 0 to `nread`.
console.log(buf.toString('utf8', 0, nread));
}
}
});