@ac-essentials/misc-util
    Preparing search index...

    Class TcpClient

    A wrapper around net.Socket that provides Promise-based methods for connecting, ending, and writing data.

    This class simplifies the use of TCP sockets by converting the callback-based methods of net.Socket into Promise-based methods, making it easier to use with async/await syntax.

    Example usage:

    const tcpClient = new TcpClient.from();
    await tcpClient.connect(80, "example.com");
    await tcpClient.write("GET / HTTP/1.1\r\nHost: example.com\r\n\r\n");
    await tcpClient.end({ waitForClose: true });
    Index

    Constructors

    Accessors

    Methods

    Constructors

    Accessors

    Methods

    • Establishes a TCP connection to the specified port and host.

      Parameters

      • port: number

        The port to connect to.

      • Optionalhost: string

        The host to connect to.

      • Optionaloptions: {
            allowHalfOpen?: boolean;
            autoSelectFamily?: boolean;
            autoSelectFamilyAttemptTimeout?: number;
            blockList?: BlockList;
            family?: number;
            fd?: number;
            hints?: number;
            keepAlive?: boolean;
            keepAliveInitialDelay?: number;
            localAddress?: string;
            localPort?: number;
            lookup?: LookupFunction;
            noDelay?: boolean;
            onread?: OnReadOpts;
            readable?: boolean;
            signal?: AbortSignal;
            timeout?: number;
            writable?: boolean;
        }

        Connection options.

        • OptionalallowHalfOpen?: boolean
        • OptionalautoSelectFamily?: boolean

          v18.13.0

        • OptionalautoSelectFamilyAttemptTimeout?: number

          v18.13.0

        • OptionalblockList?: BlockList
        • Optionalfamily?: number
        • Optionalfd?: number
        • Optionalhints?: number
        • OptionalkeepAlive?: boolean
        • OptionalkeepAliveInitialDelay?: number
        • OptionallocalAddress?: string
        • OptionallocalPort?: number
        • Optionallookup?: LookupFunction
        • OptionalnoDelay?: boolean
        • Optionalonread?: OnReadOpts
        • Optionalreadable?: boolean
        • Optionalsignal?: AbortSignal
        • Optionaltimeout?: number
        • Optionalwritable?: boolean

      Returns Promise<void>

      A promise that resolves when the connection is successfully established.

    • Half-closes the TCP connection. It sends a FIN packet to the server, indicating that no more data will be sent. The server can still send data back until it also half-closes the connection.

      Parameters

      • Optionaloptions: { waitForClose?: boolean }

        Options for disconnecting. If waitForClose is true, the promise will resolve only after the 'close' event is emitted.

      Returns Promise<void>

      A promise that resolves when the connection is half-closed or fully closed based on the provided options.

    • Writes data to the TCP socket.

      Parameters

      • data: string | Uint8Array<ArrayBufferLike>

        The data to write. Can be a string or a Uint8Array.

      • Optionaloptions: { encoding?: BufferEncoding }

        Optional write options, such as encoding.

      Returns Promise<void>

      A promise that resolves when the data is successfully written.

    • Creates a new TcpClient instance with the specified options.

      Parameters

      • Optionaloptions: SocketConstructorOpts

        Options for creating the TCP connection. Defaults to connecting to port 80.

      Returns TcpClient

      A new TcpClient instance.