hammerspoon2-docs
    Preparing search index...

    Class HSUnixDatagramSocket

    A Unix domain datagram socket, created by hs.socket.createUnixDatagram().

    A Unix domain datagram socket exchanges messages with other programs on this Mac through a socket file. Each message arrives whole, as one "data" event. Other programs can only send to it once it has been bound to a path with bind(), which creates the socket file there.

    This is what tools such as Karabiner-Elements' send_user_command send to.

    Do not instantiate HSUnixDatagramSocket directly - use hs.socket.createUnixDatagram().

    Index
    identifier: string

    A unique identifier for this socket (UUID string).

    isBound: boolean

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

    isConnected: boolean

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

    isReceiving: boolean

    Whether the socket is currently receiving datagrams.

    localPath: string | null

    The path the socket is bound to, or null if it isn't bound.

    peerPath: string | null

    The path of the peer the socket is connected to, or null if it isn't connected.

    • Binds the socket to a path, creating a socket file there that other programs can send datagrams to. Call it before connect() or sending.

      A stale socket file left at the path by a socket that has gone is replaced, but binding fails if another socket is still using it. close() removes the socket file.

      Parameters

      • path: string

        The path to create the socket file at, at most 103 bytes long, e.g. "/tmp/hammerspoon.sock".

      Returns HSUnixDatagramSocket

      The socket, for chaining.

    • Closes the socket immediately, and removes its socket file if it's bound. The socket can be bound, connected or used to send again afterwards.

      Returns HSUnixDatagramSocket

      The socket, for chaining.

    • Connects the socket to a single peer. Afterwards, send() needs no path, and only datagrams from that peer are received. The callback receives "connected" straight away.

      Parameters

      • path: string

        The path of the peer's socket file.

      Returns HSUnixDatagramSocket

      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 HSUnixDatagramSocket

      The socket, for chaining.

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

      Returns HSUnixDatagramSocket

      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 HSUnixDatagramSocket

      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.

      • Optionalpath: string | null

        The path of the socket file to send to. Omit it if the socket is connected.

      Returns HSUnixDatagramSocket

      The socket, for chaining.

    • Sends raw bytes as one datagram.

      Parameters

      • bytes: ArrayBuffer | Uint8Array<ArrayBufferLike>

        The bytes to send. Any typed array is accepted.

      • Optionalpath: string | null

        The path of the socket file to send to. Omit it if the socket is connected.

      Returns HSUnixDatagramSocket

      The socket, for chaining.

    • Sets the callback that receives this socket's events.

      The callback receives the event name, the socket, and event-specific arguments:

      • "data" - a datagram arrived; the third argument is a Uint8Array and the fourth is the sender's address, as {path}, or null if the sender's socket isn't bound to a path (so it can't be replied to).
      • "connected" - connect() succeeded; the fourth argument is the peer's address.
      • "error" - something went wrong; the third argument is a description.

      Parameters

      • callback:
            | (
                (
                    event: "error" | "connected" | "data",
                    socket: HSUnixDatagramSocket,
                    data?: string | Uint8Array<ArrayBufferLike> | null,
                    address?: { path: string } | null,
                ) => void
            )
            | null

        The event handler, or null to remove it.

      Returns HSUnixDatagramSocket

      The socket, for chaining.