WebSocket. connect Asynchronous
WebSocket.ConnectWebSocket.newsyn.websocket.Connectsyn.websocket.connectsyn.websocket.newwebsocket.Connectwebsocket.connectwebsocket.new
Connect to a WebSocket server to exchange messages over a persistent connection. The returned socket provides send and close methods plus events for incoming messages and closure.
Luau
WebSocket.connect(url: string) -> WebSocketParameters
| Parameter | Type | Description |
|---|---|---|
url | string | Server URL beginning with ws:// or wss://. |
Returns
WebSocketA connected WebSocket object. OnMessage supplies incoming messages as strings; OnClose notifies listeners that the connection has closed.
Luau
WebSocket = {
OnMessage: NativeSignal, OnClose: NativeSignal,
IsClosed: boolean
}Usage notes
This library connects as a client. Register event handlers on the returned socket and close it when it is no longer needed.
Differences from sUNC
Kawaii also exposes IsClosed on the returned socket. Send accepts an optional binary flag, documented on its method page.
Example
Luau
-- Start an echo server you control on this device, or replace the URL.
local socket = WebSocket.connect("ws://127.0.0.1:8765")
local received = socket.OnMessage:Connect(function(message)
print("Echo:", message)
end)
socket:Send("example message")
task.wait(2)
received:Disconnect()
socket:Close()