WebSocket

WebSocket.connect

Creates two BindableEvents, opens a WebSocket client WebSocket connection to the provided URL, and yields until the connection attempt finishes.

Syntax

WebSocket.connect(url: string): WebSocket
Aliases WebSocket.Connectwebsocket.connectwebsocket.Connect

Arguments

NameTypeDescription
urlstringURL string passed to CURLOPT_URL before WebSocket client performs the WebSocket connection attempt.

Returns

NameTypeDescription
socketWebSocketWebSocket table containing OnMessage, OnClose, and connection state. Send and Close are resolved with the WebSocket object methods.

Description

Creates two BindableEvents, opens a WebSocket client WebSocket connection to the provided URL, and yields until the connection attempt finishes. On success, it returns a WebSocket table with OnMessage and OnClose events, Send and Close methods supplied by the methods, and an connection state. Incoming message data is fired with OnMessage as a string. When the receive loop ends, OnClose is fired with no arguments. On connection failure, the function raises an error string that starts with "WebSocket connection failed".

Call it with 1 parameter(s): url. The argument table explains which values are required and which ones only refine the behavior.

It returns socket (WebSocket). Use the returns table to separate successful values from nil results and recoverable errors.

Example

Open a WebSocket, listen for messages and close events, then use the socket methods.

local socket = WebSocket.connect("wss://example.com/socket")

socket.OnMessage:Connect(function(message)
    print(message)
end)

socket.OnClose:Connect(function()
    print("closed")
end)

socket:Send("hello")
socket:Close()