HSUDPSocket
TypeA UDP socket, created by hs.socket.createUDP().
A UDP socket can send datagrams to any host, or be connected to a single peer, and can
receive datagrams once it has a local port: either one chosen with bind(), or one the system
assigns when it first connects or sends.
IPv4 and IPv6 are both supported, using a separate system socket for each.
Do not instantiate HSUDPSocket directly - use hs.socket.createUDP().
Properties
broadcast
boolean
Whether the socket may send to broadcast addresses (e.g. `255.255.255.255`). Defaults to
`false`. Broadcasts are IPv4 only.
reusePort
boolean
Whether other sockets may bind to the same port, e.g. so several programs can receive the
same broadcasts. Defaults to `false`. Set it before calling `bind()`.
ipv4Enabled
boolean
Whether the socket uses IPv4. Defaults to `true`. Set it before the socket binds,
connects or sends.
ipv6Enabled
boolean
Whether the socket uses IPv6. Defaults to `true`. Set it before the socket binds,
connects or sends.
Methods
setCallback(callback) -> HSUDPSocket
Sets the callback that receives this socket's events.
sender's address, as `{host, port, family}`, where family is `"ipv4"` or `"ipv6"`.
Declaration
setCallback(callback) -> HSUDPSocket
Parameters
| Name | Type | Description |
|---|---|---|
| callback | function | null | The event handler, or `null` to remove it. |
Returns
HSUDPSocket
The socket, for chaining.
Example
udp.setCallback((event, sock, data, address) => {
if (event === "data") console.log(address.host + " sent " + hs.socket.decodeText(data))
})
bind(port, address) -> HSUDPSocket
Binds the socket to a local port, so it can receive datagrams sent to that port. Call it
before `connect()` or sending.
By default the socket binds on all network interfaces. Pass one of this Mac's IP addresses
to bind only to that address (and so only to the interface that has it). Use
`hs.network.addresses()` to find an interface's addresses.
If the interface's address can change (e.g. it comes from DHCP), watch
`State:/Network/Interface/<name>/IPv4` with `hs.network.configurationWatcher()` and bind again
when it does.
Declaration
bind(port, address) -> HSUDPSocket
Parameters
| Name | Type | Description |
|---|---|---|
| port | number | The UDP port to bind to, or 0 to let the system choose one (see `localPort`). |
| address | string | A local IPv4 or IPv6 address to bind to, e.g. `"127.0.0.1"` or `"192.168.1.20"`. Defaults to all addresses. |
Returns
HSUDPSocket
The socket, for chaining.
Notes
A socket bound to an address only uses that address's family (IPv4 or IPv6), and doesn't receive broadcasts
Example
hs.socket.createUDP().setCallback(handler).bind(9999).receive()
// Only on the Wi-Fi interface's IPv4 address
const wifi = hs.network.addresses().find(a => a.interface === "en0" && a.family === "ipv4")
hs.socket.createUDP().setCallback(handler).bind(9999, wifi.address).receive()
connect(host, port) -> HSUDPSocket
Connects the socket to a single peer. Afterwards, `send()` needs no host and port, and only
datagrams from that peer are received. The callback receives `"connected"` once the host
has been looked up.
Declaration
connect(host, port) -> HSUDPSocket
Parameters
| Name | Type | Description |
|---|---|---|
| host | string | The host name or IP address of the peer. |
| port | number | The peer's UDP port. |
Returns
HSUDPSocket
The socket, for chaining.
Example
hs.socket.createUDP().connect("192.168.1.10", 9999).send("hello").receive()
send(text, host, port) -> HSUDPSocket
Sends text, encoded as UTF-8, as one datagram.
Declaration
send(text, host, port) -> HSUDPSocket
Parameters
| Name | Type | Description |
|---|---|---|
| text | string | The text to send. |
| host | string | The host name or IP address to send to. Omit it if the socket is connected. |
| port | number | The UDP port to send to. Omit it if the socket is connected. |
Returns
HSUDPSocket
The socket, for chaining.
Example
udp.send("hello", "127.0.0.1", 9999)
sendBytes(bytes, host, port) -> HSUDPSocket
Sends raw bytes as one datagram.
Declaration
sendBytes(bytes, host, port) -> HSUDPSocket
Parameters
| Name | Type | Description |
|---|---|---|
| bytes | JSValue | The bytes to send. Any typed array is accepted. |
| host | string | The host name or IP address to send to. Omit it if the socket is connected. |
| port | number | The UDP port to send to. Omit it if the socket is connected. |
Returns
HSUDPSocket
The socket, for chaining.
Example
udp.sendBytes(new Uint8Array([0xFF, 0x00]), "127.0.0.1", 9999)
receive() -> HSUDPSocket
Starts delivering every datagram that arrives to the callback, until `pause()` or
`close()`. The socket must have a local port first (see `bind()`).
Declaration
receive() -> HSUDPSocket
Returns
HSUDPSocket
The socket, for chaining.
Notes
A receiving socket, and its callback, stay alive until it stops receiving, even if you don't keep a reference to it
Example
udp.bind(9999).receive()
receiveOne() -> HSUDPSocket
Delivers the next datagram that arrives to the callback, then stops receiving.
Declaration
receiveOne() -> HSUDPSocket
Returns
HSUDPSocket
The socket, for chaining.
Notes
A receiving socket, and its callback, stay alive until it stops receiving, even if you don't keep a reference to it
Example
udp.send("ping", "127.0.0.1", 9999).receiveOne()
pause() -> HSUDPSocket
Stops receiving datagrams. Datagrams that arrive meanwhile are queued by the system (up to
its buffer size) and delivered once you receive again.
Declaration
pause() -> HSUDPSocket
Returns
HSUDPSocket
The socket, for chaining.
Example
udp.pause()
close() -> HSUDPSocket
Closes the socket immediately, discarding any sends still waiting for a host lookup. The
socket can be bound, connected or used to send again afterwards.
Declaration
close() -> HSUDPSocket
Returns
HSUDPSocket
The socket, for chaining.
Example
udp.close()
destroy() -> None
Closes the socket and releases its callback.
Declaration
destroy() -> None
Returns
None
Example
udp.destroy()