> ## Documentation Index
> Fetch the complete documentation index at: https://docs.projectreal.gg/llms.txt
> Use this file to discover all available pages before exploring further.

# WebSocket.connect

> Создает два BindableEvents, открывает WebSocket client WebSocket connection к переданному URL и выполняет yield до завершения попытки подключения. При успехе возвращает WebSocket table с events OnMessage и OnClose, methods Send и Close из methods, а также connection state. Incoming message data вызывается через OnMessage как string. Когда receive loop завершается, OnClose вызывается без arguments. При ошибке подключения функция выбрасывает error string, начинающийся с "WebSocket connection failed".

<div className="real-function-page">
  ```lua theme={null}
  WebSocket.connect(url: string): WebSocket
  ```

  ## Алиасы

  * `WebSocket.Connect`
  * `websocket.connect`
  * `websocket.Connect`

  ## Аргументы

  <ParamField path={"url"} type={"string"} required>
    URL string, передаваемый в CURLOPT\_URL перед тем, как WebSocket client выполнит WebSocket connection attempt.
  </ParamField>

  ## Возвраты

  <ResponseField name={"socket"} type={"WebSocket"}>
    WebSocket table, содержащая OnMessage, OnClose и connection state. Send и Close разрешаются через WebSocket object methods.
  </ResponseField>

  ## Пример

  Откройте WebSocket, подключите OnMessage и OnClose, затем используйте socket methods для отправки и закрытия.

  ```lua theme={null}
  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()
  ```
</div>
