HSWebSocket
TypeA 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
Methods
setOpenCallback(callback) -> HSWebSocket
Set the callback invoked when the connection is established.
Declaration
setOpenCallback(callback) -> HSWebSocket
Parameters
| Name | Type | Description |
|---|---|---|
| callback | function | null | Called when the connection opens. |
Returns
HSWebSocket
This WebSocket, for chaining.
Example
ws.setOpenCallback(() => console.log("Connected!"))
setMessageCallback(callback) -> HSWebSocket
Set the callback invoked when a text message is received from the server.
Declaration
setMessageCallback(callback) -> HSWebSocket
Parameters
| Name | Type | Description |
|---|---|---|
| callback | function | null | Called with each received message. |
Returns
HSWebSocket
This WebSocket, for chaining.
Example
ws.setMessageCallback(msg => console.log("Got: " + msg))
setCloseCallback(callback) -> HSWebSocket
Set the callback invoked when the connection is closed by the remote end.
Declaration
setCloseCallback(callback) -> HSWebSocket
Parameters
| Name | Type | Description |
|---|---|---|
| callback | function | null | Called with the WebSocket close code and reason. |
Returns
HSWebSocket
This WebSocket, for chaining.
Example
ws.setCloseCallback((code, reason) => console.log("Closed: " + code))
setErrorCallback(callback) -> HSWebSocket
Set the callback invoked when a connection or protocol error occurs.
Declaration
setErrorCallback(callback) -> HSWebSocket
Parameters
| Name | Type | Description |
|---|---|---|
| callback | function | null | Called with the error description. |
Returns
HSWebSocket
This WebSocket, for chaining.
Example
ws.setErrorCallback(err => console.log("Error: " + err))
send(message) -> HSWebSocket
Send a text message to the server.
The connection must be open (`readyState === 1`).
Declaration
send(message) -> HSWebSocket
Parameters
| Name | Type | Description |
|---|---|---|
| message | string | The text message to send. |
Returns
HSWebSocket
This WebSocket, for chaining.
Example
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.
Declaration
close() -> None
Returns
None
Example
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.
Declaration
destroy() -> None
Returns
None
Example
ws.destroy()