hammerspoon2-docs
    Preparing search index...

    Class HSWebSocket

    A WebSocket client connection created by hs.http.openWebSocket(). The connection opens immediately when returned. Use the chainable setter methods to register event callbacks, then call send() to transmit messages. Do not instantiate HSWebSocket directly — use hs.http.openWebSocket().

    Index

    Constructors

    Properties

    identifier: string

    A unique identifier for this connection (UUID string).

    readyState: number

    The current connection state.

    Methods

    • Close the WebSocket connection with a normal closure code (1000). If a close callback is registered, it is invoked synchronously.

      Returns void

    • Destroy this WebSocket, releasing all resources without invoking callbacks. Called automatically by hs.http.shutdown(). After destroy(), do not use this object.

      Returns void

    • Send a text message to the server. The connection must be open (readyState === 1).

      Parameters

      • message: string

        The text message to send.

      Returns HSWebSocket

      This WebSocket, for chaining.

    • Set the callback invoked when the connection is closed by the remote end.

      Parameters

      • callback: (code: number, reason: string) => void

        Called with the WebSocket close code and reason.

      Returns HSWebSocket

      This WebSocket, for chaining.

    • Set the callback invoked when a connection or protocol error occurs.

      Parameters

      • callback: (error: string) => void

        Called with the error description.

      Returns HSWebSocket

      This WebSocket, for chaining.

    • Set the callback invoked when a text message is received from the server.

      Parameters

      • callback: (message: string) => void

        Called with each received message.

      Returns HSWebSocket

      This WebSocket, for chaining.

    • Set the callback invoked when the connection is established.

      Parameters

      • callback: () => void

        Called when the connection opens.

      Returns HSWebSocket

      This WebSocket, for chaining.