WebSocket.connect
Creates a new WebSocket connection to the specified URL.
Syntax
WebSocket.connect(url: string) -> WebSocketParameters
| Parameter | Type | Description |
|---|---|---|
url | string | The WebSocket URL to connect to |
Returns
| Type | Description |
|---|---|
WebSocket | A WebSocket object with methods and events |
Description
WebSocket.connect yields while Volt establishes a connection. It returns a WebSocket object after a successful handshake and raises an error if the connection fails.
Example
-- Replace this with a WebSocket endpoint you control
local ws = WebSocket.connect("wss://your-server.example/socket")
-- Handle incoming messages
ws.OnMessage:Connect(function(message, isBinary)
print("Received:", message, "binary:", isBinary)
end)
-- Handle connection close
ws.OnClose:Connect(function()
print("Connection closed")
end)
-- Send a message
ws:Send("Hello, WebSocket!")Notes
- Use a
ws://orwss://WebSocket URL;wss://encrypts the connection - Volt attempts to reconnect automatically after a remote disconnection
- Call
Closeto stop the client intentionally
Related
WebSocket:Send- Send messagesWebSocket:Close- Close connectionOnMessage- Receive messages