hammerspoon2-docs
    Preparing search index...

    Class HSUDPSocket

    A 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().

    Index

    Constructors

    Properties

    broadcast: boolean

    Whether the socket may send to broadcast addresses (e.g. 255.255.255.255). Defaults to false. Broadcasts are IPv4 only.

    identifier: string

    A unique identifier for this socket (UUID string).

    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.

    isBound: boolean

    Whether the socket has been bound to a local port with bind().

    isConnected: boolean

    Whether the socket is connected to a single peer with connect().

    isReceiving: boolean

    Whether the socket is currently receiving datagrams.

    localPort: number

    The local port the socket is using, or 0 if it doesn't have one yet.

    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().

    Methods

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

      Parameters

      • port: number

        The UDP port to bind to, or 0 to let the system choose one (see localPort).

      • Optionaladdress: string | null

        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.

      A socket bound to an address only uses that address's family (IPv4 or IPv6), and doesn't receive broadcasts

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

      Returns HSUDPSocket

      The socket, for chaining.

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

      Parameters

      • host: string

        The host name or IP address of the peer.

      • port: number

        The peer's UDP port.

      Returns HSUDPSocket

      The socket, for chaining.

    • Closes the socket and releases its callback.

      Returns void

    • Stops receiving datagrams. Datagrams that arrive meanwhile are queued by the system (up to its buffer size) and delivered once you receive again.

      Returns HSUDPSocket

      The socket, for chaining.

    • Starts delivering every datagram that arrives to the callback, until pause() or close(). The socket must have a local port first (see bind()).

      Returns HSUDPSocket

      The socket, for chaining.

      A receiving socket, and its callback, stay alive until it stops receiving, even if you don't keep a reference to it

    • Delivers the next datagram that arrives to the callback, then stops receiving.

      Returns HSUDPSocket

      The socket, for chaining.

      A receiving socket, and its callback, stay alive until it stops receiving, even if you don't keep a reference to it

    • Sends text, encoded as UTF-8, as one datagram.

      Parameters

      • text: string

        The text to send.

      • Optionalhost: string | null

        The host name or IP address to send to. Omit it if the socket is connected.

      • Optionalport: number

        The UDP port to send to. Omit it if the socket is connected.

      Returns HSUDPSocket

      The socket, for chaining.

    • Sends raw bytes as one datagram.

      Parameters

      • bytes: ArrayBuffer | Uint8Array<ArrayBufferLike>

        The bytes to send. Any typed array is accepted.

      • Optionalhost: string | null

        The host name or IP address to send to. Omit it if the socket is connected.

      • Optionalport: number

        The UDP port to send to. Omit it if the socket is connected.

      Returns HSUDPSocket

      The socket, for chaining.

    • Sets the callback that receives this socket's events. sender's address, as {host, port, family}, where family is "ipv4" or "ipv6".

      Parameters

      • callback:
            | (
                (
                    event: "connected" | "data" | "error",
                    socket: HSUDPSocket,
                    data?: string | Uint8Array<ArrayBufferLike> | null,
                    address?: { family: string; host: string; port: number },
                ) => void
            )
            | null

        The event handler, or null to remove it.

      Returns HSUDPSocket

      The socket, for chaining.