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

    Class TcpSocket<TSock, TEvents>

    TCP socket for establishing connections to TCP servers.

    Example usage:

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

    Type Parameters

    Hierarchy (View Summary)

    Index

    Constructors

    Properties

    handledErrorEvents: Set<Error> = ...
    sock: TSock

    The underlying Node.js socket.

    Accessors

    • get stream(): Duplex

      Underlying duplex stream for reading and writing data.

      This is the wrapped net.Socket instance and can be passed directly to APIs that expect a Node.js stream.

      Returns Duplex

    • get timeout(): number | null

      Current inactivity timeout in milliseconds, or null if disabled.

      Returns number | null

    • set timeout(timeout: number | null): void

      Updates the inactivity timeout for the socket.

      When set to a positive number, the socket emits a "timeout" event if no I/O activity occurs within the given number of milliseconds. A value of 0 or null disables the timeout entirely.

      Parameters

      • timeout: number | null

      Returns void

    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 (defaults to localhost).

      • Optionaloptions: {
            autoSelectFamily?: boolean;
            autoSelectFamilyAttemptTimeout?: number;
            blockList?: BlockList;
            family?: number;
            hints?: number;
            keepAlive?: boolean;
            keepAliveInitialDelay?: number;
            localAddress?: string;
            localPort?: number;
            lookup?: LookupFunction;
            noDelay?: boolean;
        }

        Connection options.

        • OptionalautoSelectFamily?: boolean

          v18.13.0

        • OptionalautoSelectFamilyAttemptTimeout?: number

          v18.13.0

        • OptionalblockList?: BlockList
        • Optionalfamily?: number
        • Optionalhints?: number
        • OptionalkeepAlive?: boolean
        • OptionalkeepAliveInitialDelay?: number
        • OptionallocalAddress?: string
        • OptionallocalPort?: number
        • Optionallookup?: LookupFunction
        • OptionalnoDelay?: boolean

      Returns Promise<void>

      A promise that resolves when the connection is successfully established.

    • Half-closes the socket, optionally waiting for a full close.

      If waitForClose is omitted or false, the promise resolves once the local side has finished sending data and the FIN has been queued. When waitForClose is true, the promise resolves only after the remote side has also closed and the "close" event has fired.

      Parameters

      • Optionaloptions: { waitForClose?: boolean }

        Optional settings for ending the socket.

      Returns Promise<void>

      A promise that resolves once the socket has ended.

    • Sets the keep-alive option for the socket.

      Parameters

      • enable: boolean

        Whether to enable keep-alive.

      • OptionalinitialDelay: number

        The initial delay in milliseconds before the first keep-alive probe.

      Returns void

    • Creates a new TcpSocket instance.

      Parameters

      • Optionaloptions: {
            allowHalfOpen?: boolean;
            onread?: OnReadOpts;
            readable?: boolean;
            signal?: AbortSignal;
            writable?: boolean;
        }

        Options for creating the underlying Node.js socket.

      Returns TcpSocket

      A new TcpSocket instance.