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

    Class UdpSocket

    A wrapper around dgram.Socket that provides Promise-based methods for binding, closing, and sending data.

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

    Example usage:

    const udpSocket = new UdpSocket.from({ type: "udp4" });
    await udpSocket.bind({ address: "localhost", port: 12345 });
    await udpSocket.send(Buffer.from("Hello, UDP!"), 0, 13, 54321, "localhost");
    await udpSocket.close();
    Index

    Constructors

    Accessors

    Methods

    Constructors

    Accessors

    Methods

    • Binds the UDP socket to the specified address and port.

      Parameters

      • port: number = 0

        The port to bind to. If not specified, a random available port will be used.

      • Optionaladdress: string

        The address to bind to. If not specified, the socket will bind to all available interfaces.

      • Optionaloptions: { exclusive?: boolean; fd?: number }

        Binding options.

      Returns Promise<void>

      A promise that resolves when the socket is successfully bound.

    • Sends a message to the specified address and port.

      The msg argument contains the message to be sent. Depending on its type, different behavior can apply. If msg is a Buffer, any TypedArray or a DataView, the offset and length specify the offset within the Buffer where the message begins and the number of bytes in the message, respectively.

      If msg is a String, then it is automatically converted to a Buffer with 'utf8' encoding.

      With messages that contain multi-byte characters, offset and length will be calculated with respect to byte length and not the character position. If msg is an array, offset and length must not be specified.

      Parameters

      • port: number

        The destination port.

      • address: string

        The destination address.

      • msg: string | TypedArray | DataView<ArrayBufferLike>

        The message to send. This can be a Buffer, TypedArray, or DataView.

      • Optionaloffset: number

        Optional offset in the message buffer to start sending from.

      • Optionallength: number

        Optional number of bytes to send from the message buffer.

      Returns Promise<number>

      A promise that resolves with the number of bytes sent.