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
Inherited from
Properties
| Property | Modifier | Type | Description |
|---|---|---|---|
address | readonly | object | Internal The local address information of the bound socket. Set by the native $.udpInit via a getter on the prototype. |
address.address | public | string | - |
address.port | public | number | - |
fd | readonly | number | Internal 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
| Parameter | Type |
|---|---|
type | string |
callback | null | EventListenerOrEventListenerObject<any> |
options? | boolean | AddEventListenerOptions |
Returns
void
Inherited from
See
https://developer.mozilla.org/docs/Web/API/EventTarget/addEventListener
addMembership()
addMembership(multicastAddress, multicastInterface?): void
Join a multicast group.
Parameters
| Parameter | Type | Description |
|---|---|---|
multicastAddress | string | The multicast group address to join (e.g. "239.255.255.250"). |
multicastInterface? | string | The 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
| Parameter | Type |
|---|---|
event | Event |
Returns
boolean
Inherited from
See
https://developer.mozilla.org/docs/Web/API/EventTarget/dispatchEvent
dropMembership()
dropMembership(multicastAddress, multicastInterface?): void
Leave a multicast group.
Parameters
| Parameter | Type | Description |
|---|---|---|
multicastAddress | string | The multicast group address to leave. |
multicastInterface? | string | The 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
| Parameter | Type |
|---|---|
type | string |
callback | null | 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
| Parameter | Type | Description |
|---|---|---|
data | string | Uint8Array | ArrayBuffer | The data to send. Strings are encoded as UTF-8. |
remoteAddress | string | The destination IP address. |
remotePort | number | The destination port number. |
Returns
Promise<number>
setBroadcast()
setBroadcast(enabled): void
Enable or disable broadcast on this socket.
Parameters
| Parameter | Type | Description |
|---|---|---|
enabled | boolean | Whether to enable broadcast. |
Returns
void