nx.js
NamespacesSwitchClasses

DatagramSocket

A UDP datagram socket bound to a local address and port.

Uses the Web EventTarget API. Dispatches DatagramEvent instances when datagrams are received.

Example

const socket = Switch.listenDatagram({ port: 9999 });

socket.addEventListener('message', (e) => {
  console.log(`From ${e.remoteAddress}:${e.remotePort}:`, e.data);
  socket.send('pong', e.remoteAddress, e.remotePort);
});

Extends

Constructors

new DatagramSocket()

new DatagramSocket(): DatagramSocket

Returns

DatagramSocket

Inherited from

EventTarget.constructor

Properties

PropertyModifierTypeDescription
addressreadonlyobjectInternal The local address information of the bound socket. Set by the native $.udpInit via a getter on the prototype.
address.addresspublicstring-
address.portpublicnumber-
fdreadonlynumberInternal The file descriptor of the underlying socket. Set by the native $.udpInit via a getter on the prototype.

Methods

addEventListener()

addEventListener(type, callback, options?): void

Appends an event listener for events whose type attribute value is type. The callback argument sets the callback that will be invoked when the event is dispatched.

The options argument sets listener-specific options. For compatibility this can be a boolean, in which case the method behaves exactly as if the value was specified as options's capture.

When set to true, options's capture prevents callback from being invoked when the event's eventPhase attribute value is BUBBLING_PHASE. When false (or not present), callback will not be invoked when event's eventPhase attribute value is CAPTURING_PHASE. Either way, callback will be invoked if event's eventPhase attribute value is AT_TARGET.

When set to true, options's passive indicates that the callback will not cancel the event by invoking preventDefault(). This is used to enable performance optimizations described in § 2.8 Observing event listeners.

When set to true, options's once indicates that the callback will only be invoked once after which the event listener will be removed.

If an AbortSignal is passed for options's signal, then the event listener will be removed when signal is aborted.

The event listener is appended to target's event listener list and is not appended if it has the same type, callback, and capture.

Parameters

ParameterType
typestring
callbacknull | EventListenerOrEventListenerObject<any>
options?boolean | AddEventListenerOptions

Returns

void

Inherited from

EventTarget.addEventListener

See

https://developer.mozilla.org/docs/Web/API/EventTarget/addEventListener


addMembership()

addMembership(multicastAddress, multicastInterface?): void

Join a multicast group.

Parameters

ParameterTypeDescription
multicastAddressstringThe multicast group address to join (e.g. "239.255.255.250").
multicastInterface?stringThe local interface address to use. Defaults to "0.0.0.0" (any interface).

Returns

void


close()

close(): void

Close the socket. No more datagrams will be received or sent.

Returns

void


dispatchEvent()

dispatchEvent(event): boolean

Dispatches a synthetic event event to target and returns true if either event's cancelable attribute value is false or its preventDefault() method was not invoked, and false otherwise.

Parameters

ParameterType
eventEvent

Returns

boolean

Inherited from

EventTarget.dispatchEvent

See

https://developer.mozilla.org/docs/Web/API/EventTarget/dispatchEvent


dropMembership()

dropMembership(multicastAddress, multicastInterface?): void

Leave a multicast group.

Parameters

ParameterTypeDescription
multicastAddressstringThe multicast group address to leave.
multicastInterface?stringThe local interface address. Defaults to "0.0.0.0".

Returns

void


removeEventListener()

removeEventListener(type, callback, options?): void

Removes the event listener in target's event listener list with the same type, callback, and options.

Parameters

ParameterType
typestring
callbacknull | EventListenerOrEventListenerObject<any>
options?boolean | EventListenerOptions

Returns

void

Inherited from

EventTarget.removeEventListener

See

https://developer.mozilla.org/docs/Web/API/EventTarget/removeEventListener


send()

send(data, remoteAddress, remotePort): Promise<number>

Send a datagram to the specified address and port.

Parameters

ParameterTypeDescription
datastring | Uint8Array | ArrayBufferThe data to send. Strings are encoded as UTF-8.
remoteAddressstringThe destination IP address.
remotePortnumberThe destination port number.

Returns

Promise<number>


setBroadcast()

setBroadcast(enabled): void

Enable or disable broadcast on this socket.

Parameters

ParameterTypeDescription
enabledbooleanWhether to enable broadcast.

Returns

void

On this page