new net.Socket([options])


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

    ¥options <Object> Available options are:

    • allowHalfOpen <boolean> 如果设置为 false,则当可读端结束时,套接字将自动结束可写端。有关详细信息,请参阅 net.createServer()'end' 事件。默认值:false

      ¥allowHalfOpen <boolean> If set to false, then the socket will automatically end the writable side when the readable side ends. See net.createServer() and the 'end' event for details. Default: false.

    • fd <number> 如果指定,则使用给定的文件描述符封装现有的套接字,否则将创建新的套接字。

      ¥fd <number> If specified, wrap around an existing socket with the given file descriptor, otherwise a new socket will be created.

    • onread <Object> 如果指定,传入的数据存储在单个 buffer 中,并在数据到达套接字时传给提供的 callback。这将导致流式传输功能不提供任何数据。套接字将像往常一样触发 'error''end''close' 等事件。pause()resume() 之类的方法也将按预期运行。

      ¥onread <Object> If specified, incoming data is stored in a single buffer and passed to the supplied callback when 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 like pause() and resume() 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 to buffer and a reference to buffer. Return false from this function to implicitly pause() the socket. This function will be executed in the global context.

    • readable <boolean> 当传入 fd 时,则允许在套接字上读取,否则将被忽略。默认值:false

      ¥readable <boolean> Allow reads on the socket when an fd is passed, otherwise ignored. Default: false.

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

      ¥signal <AbortSignal> An Abort signal that may be used to destroy the socket.

    • writable <boolean> 当传入 fd 时,则允许在套接字上写入,否则将被忽略。默认值:false

      ¥writable <boolean> Allow writes on the socket when an fd is passed, otherwise ignored. Default: false.

  • 返回:<net.Socket>

    ¥Returns: <net.Socket>

创建一个新的套接字对象。

¥Creates a new socket object.

新创建的套接字可以是 TCP 套接字或流式 IPC 端点,具体取决于它 connect() 的用途。

¥The newly created socket can be either a TCP socket or a streaming IPC endpoint, depending on what it connect() to.