Volt

WebSocket.OnClose

Signal that fires when the WebSocket connection is closed.

Syntax

WebSocket.OnClose:Connect(callback: () -> ()) -> Connection

Callback Parameters

This callback receives no parameters.

Returns

TypeDescription
ConnectionA connection that can be disconnected

Description

OnClose is a signal that fires when the WebSocket connection is terminated, either by calling Close() or when the server disconnects.

Example

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

ws.OnClose:Connect(function()
    print("Connection closed")
end)

-- Later...
ws:Close()
-- Output: Connection closed

Reconnection Pattern

local function createConnection()
    local ws = WebSocket.connect("wss://example.com/socket")
    
    ws.OnClose:Connect(function()
        print("Disconnected, reconnecting in 5 seconds...")
        task.wait(5)
        createConnection()
    end)
    
    ws.OnMessage:Connect(function(msg)
        print("Message:", msg)
    end)
    
    return ws
end

local connection = createConnection()

Cleanup on Close

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

ws.OnClose:Connect(function()
    isConnected = false
    print("Cleaning up...")
    -- Perform cleanup tasks
end)

-- Check connection state before sending
if isConnected then
    ws:Send("Hello!")
end

On this page