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.
setOpenCallback(callback) -> HSWebSocket
| Name | Type | Description |
|---|---|---|
| callback | function | null | Called when the connection opens. |
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. |
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. |
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. |
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. |
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
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
ws.destroy()