WebSocket.connect
Opens a WebSocket and returns a socket object.
Syntax
WebSocket.connect(url: string) -> table
Parameters
| Parameter | Type | Description |
|---|---|---|
url | string | ws:// or wss:// address to open. |
Returns
| Type | Description |
|---|---|
table | A socket object with Send, Close, OnMessage and OnClose members. |
Description
WebSocket.connect and WebSocket.open open a WinHTTP WebSocket and return a socket table. Connect, Open, New and new are aliases of the same function.
The socket exposes Send/send, Close/close, OnMessage/onMessage, OnClose/onClose and IsClosed/isClosed. Lowercase names are the modern aliases and uppercase names remain for compatibility.
Example
local ws = WebSocket.open("wss://echo.websocket.events")
ws.OnMessage:Connect(function(message)
print(message)
end)
ws:Send("hello")
Notes
- Aliases:
WebSocket.open(url),WebSocket.Connect(url),WebSocket.Open(url),WebSocket.New(url),WebSocket.new(url). - Keep message callbacks short; they run on the module's networking thread.
- Accepts only ws:// and wss:// URLs.
- Incoming messages are delivered through OnMessage.