WebSocket:Close

Closes the WebSocket connection.

Syntax

WebSocket:Close() -> ()

Parameters

This method takes no parameters.

Returns

This method does not return a value.

Description

WebSocket:Close closes the connection. IsClosed becomes true and OnClose fires when closing completes. Later Send and Close calls have no effect.

Example

local ws = WebSocket.connect("wss://your-server.example/socket")

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

-- Use the connection
ws:Send("Hello!")

-- Close after 10 seconds
task.wait(10)
ws:Close()

Cleanup Pattern

local ws = WebSocket.connect("wss://your-server.example/socket")

local function cleanup()
    if ws then
        ws:Close()
        ws = nil
    end
end

-- Close on script end or when done
cleanup()

Notes

  • A locally closed client cannot be reopened
  • Close is safe to call multiple times
  • Use IsClosed to inspect the local closed state