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

    Class DgramSocket

    High-level wrapper around Node.js dgram.Socket | UDP datagram sockets.

    This class delegates almost all behavior to an underlying dgram.Socket instance while:

    Unless explicitly documented otherwise, methods have the same semantics as their counterparts on dgram.Socket.

    Example usage:

    const dgramSocket = DgramSocket.from({ type: "udp4" });
    await dgramSocket.bind(12345, "localhost");
    dgramSocket.on("message", (msg, msgSize, from) => {
    console.log(`Received ${msgSize} bytes from ${from.address}:${from.port}`);
    });
    await dgramSocket.send(54321, "localhost", Buffer.from("Hello, UDP!"));
    await dgramSocket.close();

    Hierarchy

    Implements

    Index

    Constructors

    Methods

    • Adds this socket to the given multicast group.

      This is a thin wrapper around dgram.Socket.addMembership, which configures IP_ADD_MEMBERSHIP for the given multicast group.

      Parameters

      • multicastAddress: string
      • OptionalmulticastInterface: string

      Returns void

    • Adds this socket to a source-specific multicast group.

      This is a thin wrapper around dgram.Socket.addSourceSpecificMembership, configuring source-specific multicast membership for the given source and group.

      Parameters

      • sourceAddress: string
      • groupAddress: string
      • OptionalmulticastInterface: string

      Returns void

    • 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.

    • Removes this socket from the given multicast group.

      This is a thin wrapper around dgram.Socket.dropMembership, which configures IP_DROP_MEMBERSHIP for the given multicast group.

      Parameters

      • multicastAddress: string
      • OptionalmulticastInterface: string

      Returns void

    • Removes this socket from a source-specific multicast group.

      This is a thin wrapper around dgram.Socket.dropSourceSpecificMembership, removing source-specific multicast membership for the given source and group.

      Parameters

      • sourceAddress: string
      • groupAddress: string
      • OptionalmulticastInterface: string

      Returns void

    • Returns the size in bytes of the underlying receive buffer.

      This is a thin wrapper around dgram.Socket.getRecvBufferSize, which reads the SO_RCVBUF socket option.

      Returns number

    • Returns the size in bytes of the underlying send buffer.

      This is a thin wrapper around dgram.Socket.getSendBufferSize, which reads the SO_SNDBUF socket option.

      Returns number

    • 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.

    • Enables or disables sending of broadcast datagrams.

      This is a thin wrapper around dgram.Socket.setBroadcast, which toggles the SO_BROADCAST socket option. When enabled, UDP packets may be sent to broadcast addresses.

      Parameters

      • flag: boolean

        Whether broadcast should be enabled.

      Returns void

    • Sets the outbound multicast interface.

      This is a thin wrapper around dgram.Socket.setMulticastInterface, which controls the default outgoing interface for multicast traffic.

      Parameters

      • multicastInterface: string

      Returns void

    • Enables or disables loopback for multicast packets sent from this socket.

      This is a thin wrapper around dgram.Socket.setMulticastLoopback, which toggles the IP_MULTICAST_LOOP socket option.

      Parameters

      • flag: boolean

      Returns void

    • Sets the time-to-live (TTL) value for multicast packets.

      This is a thin wrapper around dgram.Socket.setMulticastTTL, which configures the IP_MULTICAST_TTL socket option.

      Parameters

      • ttl: number

      Returns void

    • Sets the size in bytes of the underlying receive buffer.

      This is a thin wrapper around dgram.Socket.setRecvBufferSize, which sets the SO_RCVBUF socket option.

      Parameters

      • size: number

      Returns void

    • Sets the size in bytes of the underlying send buffer.

      This is a thin wrapper around dgram.Socket.setSendBufferSize, which sets the SO_SNDBUF socket option.

      Parameters

      • size: number

      Returns void

    • Sets the unicast time-to-live (TTL) value for outgoing packets.

      This is a thin wrapper around dgram.Socket.setTTL, which configures the IP_TTL socket option.

      Parameters

      • ttl: number

      Returns void