dgram.createSocket(options[, callback])


  • options <Object> 可用的选项有:

    ¥options <Object> Available options are:

    • type <string> 套接字家族。必须是 'udp4''udp6'。必需的。

      ¥type <string> The family of socket. Must be either 'udp4' or 'udp6'. Required.

    • reuseAddr <boolean>true 时,socket.bind() 将重用该地址,即使另一个进程已在其上绑定了套接字,但只有一个套接字可以接收数据。默认值:false

      ¥reuseAddr <boolean> When true socket.bind() will reuse the address, even if another process has already bound a socket on it, but only one socket can receive the data. Default: false.

    • reusePort <boolean>true 时,socket.bind() 将重用该端口,即使另一个进程已在其上绑定了套接字。传入数据报分发到监听套接字。该选项仅在某些平台上可用,例如 Linux 3.9+、DragonFlyBSD 3.6+、FreeBSD 12.0+、Solaris 11.4 和 AIX 7.2.5+。在未支持的平台上,当套接字绑定时,此选项会引发错误。默认值:false

      ¥reusePort <boolean> When true socket.bind() will reuse the port, even if another process has already bound a socket on it. Incoming datagrams are distributed to listening sockets. The option is available only on some platforms, such as Linux 3.9+, DragonFlyBSD 3.6+, FreeBSD 12.0+, Solaris 11.4, and AIX 7.2.5+. On unsupported platforms this option raises an an error when the socket is bound. Default: false.

    • ipv6Only <boolean>ipv6Only 设置为 true 将禁用双栈支持,即绑定到地址 :: 不会使 0.0.0.0 被绑定。默认值:false

      ¥ipv6Only <boolean> Setting ipv6Only to true will disable dual-stack support, i.e., binding to address :: won't make 0.0.0.0 be bound. Default: false.

    • recvBufferSize <number> 设置 SO_RCVBUF 套接字值。

      ¥recvBufferSize <number> Sets the SO_RCVBUF socket value.

    • sendBufferSize <number> 设置 SO_SNDBUF 套接字值。

      ¥sendBufferSize <number> Sets the SO_SNDBUF socket value.

    • lookup <Function> 自定义查找函数。默认值:dns.lookup()

      ¥lookup <Function> Custom lookup function. Default: dns.lookup().

    • signal <AbortSignal> 可用于关闭套接字的中止信号。

      ¥signal <AbortSignal> An AbortSignal that may be used to close a socket.

    • receiveBlockList <net.BlockList> receiveBlockList 可用于丢弃到特定 IP 地址、IP 范围或 IP 子网的入站数据报。如果服务器位于反向代理、NAT 等后面,则这不起作用,因为针对阻止列表检查的地址是代理的地址,或 NAT 指定的地址。

      ¥receiveBlockList <net.BlockList> receiveBlockList can be used for discarding inbound datagram to specific IP addresses, IP ranges, or IP subnets. This does not work if the server is behind a reverse proxy, NAT, etc. because the address checked against the blocklist is the address of the proxy, or the one specified by the NAT.

    • sendBlockList <net.BlockList> sendBlockList 可用于禁用对特定 IP 地址、IP 范围或 IP 子网的出站访问。

      ¥sendBlockList <net.BlockList> sendBlockList can be used for disabling outbound access to specific IP addresses, IP ranges, or IP subnets.

  • callback <Function> 绑定作为 'message' 事件的监听器。可选的。

    ¥callback <Function> Attached as a listener for 'message' events. Optional.

  • 返回:<dgram.Socket>

    ¥Returns: <dgram.Socket>

创建 dgram.Socket 对象。一旦创建了套接字,则调用 socket.bind() 将指示套接字开始监听数据报消息。当 addressport 未传递给 socket.bind() 时,该方法会将套接字绑定到随机端口上的 "所有接口" 地址(它对 udp4udp6 套接字都做正确的事情)。可以使用 socket.address().addresssocket.address().port 检索绑定的地址和端口。

¥Creates a dgram.Socket object. Once the socket is created, calling socket.bind() will instruct the socket to begin listening for datagram messages. When address and port are not passed to socket.bind() the method will bind the socket to the "all interfaces" address on a random port (it does the right thing for both udp4 and udp6 sockets). The bound address and port can be retrieved using socket.address().address and socket.address().port.

如果启用了 signal 选项,则在对应的 AbortController 上调用 .abort() 与在套接字上调用 .close() 类似:

¥If the signal option is enabled, calling .abort() on the corresponding AbortController is similar to calling .close() on the socket:

const controller = new AbortController();
const { signal } = controller;
const server = dgram.createSocket({ type: 'udp4', signal });
server.on('message', (msg, rinfo) => {
  console.log(`server got: ${msg} from ${rinfo.address}:${rinfo.port}`);
});
// Later, when you want to close the server.
controller.abort();