WebSocket class
The WebSocket
class provides a lightweight interface for establishing and working with WebSocket connections. It allows scripts to send and receive messages over a persistent connection to a WebSocket server.
Keep in mind that this is client-only, meaning you won't be able to create a WebSocket server.
Constructor
Attempts to create a new connection to the provided URL. The URL must be a valid WebSocket server URL, typically starting with ws://
(unsecure) or wss://
(secure).
Parameters
Parameter | Description |
---|---|
url |
A WebSocket URL. |
Events
Signals that allow you handle events that occur during the WebSocket's lifetime, such as opening, receiving messages, or closing.
Event | Description |
---|---|
OnMessage(message: string): () |
Triggered when a message is received over the WebSocket connection. |
OnClose(): () |
Triggered when the WebSocket connection closes. |
Methods
Method | Description |
---|---|
Send(message: string): () |
Send a message over the WebSocket connection. |
Close(): () |
Closes the WebSocket connection. |
Examples
Using the OnMessage
event, and Send
method
Responding to incoming messages | |
---|---|