Constructs a new UdpSocket
instance wrapping the provided dgram.Socket
.
Binds the UDP socket to the specified address and port.
The port to bind to. If not specified, a random available port will be used.
Optional
address: stringThe address to bind to. If not specified, the socket will bind to all available interfaces.
Optional
options: { exclusive?: boolean; fd?: number }Binding options.
A promise that resolves when the socket is successfully bound.
Closes the UDP socket.
A promise that resolves when the socket is successfully closed.
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.
The destination port.
The destination address.
The message to send. This can be a Buffer
, TypedArray
, or DataView
.
Optional
offset: numberOptional offset in the message buffer to start sending from.
Optional
length: numberOptional number of bytes to send from the message buffer.
A promise that resolves with the number of bytes sent.
Static
fromCreates a new UdpSocket
instance with the specified options.
Optional
options: SocketOptions
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: