API Docs

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

Properties

identifier

string
A unique identifier for this connection (UUID string).

readyState

number
The current connection state.

Methods

setOpenCallback(callback) -> HSWebSocket

Set the callback invoked when the connection is established.
setOpenCallback(callback) -> HSWebSocket
Name Type Description
callback function | null Called when the connection opens.
HSWebSocket
This WebSocket, for chaining.
ws.setOpenCallback(() => console.log("Connected!"))

setMessageCallback(callback) -> HSWebSocket

Set the callback invoked when a text message is received from the server.
setMessageCallback(callback) -> HSWebSocket
Name Type Description
callback function | null Called with each received message.
HSWebSocket
This WebSocket, for chaining.
ws.setMessageCallback(msg => console.log("Got: " + msg))

setCloseCallback(callback) -> HSWebSocket

Set the callback invoked when the connection is closed by the remote end.
setCloseCallback(callback) -> HSWebSocket
Name Type Description
callback function | null Called with the WebSocket close code and reason.
HSWebSocket
This WebSocket, for chaining.
ws.setCloseCallback((code, reason) => console.log("Closed: " + code))

setErrorCallback(callback) -> HSWebSocket

Set the callback invoked when a connection or protocol error occurs.
setErrorCallback(callback) -> HSWebSocket
Name Type Description
callback function | null Called with the error description.
HSWebSocket
This WebSocket, for chaining.
ws.setErrorCallback(err => console.log("Error: " + err))

send(message) -> HSWebSocket

Send a text message to the server. The connection must be open (`readyState === 1`).
send(message) -> HSWebSocket
Name Type Description
message string The text message to send.
HSWebSocket
This WebSocket, for chaining.
ws.send("Hello, server!")

close() -> None

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

destroy() -> None

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