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

# HCWebSocketConnectFunction

> Callback that connects a WebSocket when you replace the libHttpClient WebSocket implementation.

# HCWebSocketConnectFunction

Connects a WebSocket when you replace the libHttpClient WebSocket implementation. libHttpClient calls this callback when the title calls [HCWebSocketConnectAsync](https://docs.xbox.com/reference/live/httpclient/httpclient/functions/hcwebsocketconnectasync.md).

## Syntax

```cpp theme={null}
HRESULT CALLBACK HCWebSocketConnectFunction(
         const char* uri,
         const char* subProtocol,
         HCWebsocketHandle websocket,
         XAsyncBlock* asyncBlock,
         void* context,
         HCPerformEnv env
)
```

### Parameters

*uri*   \_In\_z\_\
Type: const char\*

The UTF-8 encoded URI to connect to.

*subProtocol*   \_In\_z\_\
Type: const char\*

The UTF-8 encoded subprotocol to request.

*websocket*   \_In\_\
Type: HCWebsocketHandle

The handle of the WebSocket. Use this handle with the provider functions, such as [HCWebSocketGetHeaderAtIndex](https://docs.xbox.com/reference/live/httpclient/httpprovider/functions/hcwebsocketgetheaderatindex.md), [HCWebSocketGetProxyUri](https://docs.xbox.com/reference/live/httpclient/httpprovider/functions/hcwebsocketgetproxyuri.md), and [HCWebSocketGetPingInterval](https://docs.xbox.com/reference/live/httpclient/httpprovider/functions/hcwebsocketgetpinginterval.md).

*asyncBlock*   \_Inout\_\
Type: XAsyncBlock\*

The async block for the connect operation.

*context*   \_In\_opt\_\
Type: void\*

The `context` value that you passed to [HCSetWebSocketFunctions](https://docs.xbox.com/reference/live/httpclient/httpprovider/functions/hcsetwebsocketfunctions.md).

*env*   \_In\_opt\_\
Type: HCPerformEnv

An opaque value reserved for the default implementation. Ignore it.

### Return value

Type: HRESULT

Return `S_OK` if the connect operation started. If you return a failure code, the connect operation fails with that code.

## Remarks

Register this callback, together with the other three WebSocket callbacks, by calling [HCSetWebSocketFunctions](https://docs.xbox.com/reference/live/httpclient/httpprovider/functions/hcsetwebsocketfunctions.md) before [HCInitialize](https://docs.xbox.com/reference/live/httpclient/httpclient/functions/hcinitialize.md). HCSetWebSocketFunctions requires all four callbacks; it returns `E_INVALIDARG` if any of them is `nullptr`.

libHttpClient reads the result of the connect operation by calling [HCGetWebSocketConnectResult](https://docs.xbox.com/reference/live/httpclient/httpclient/functions/hcgetwebsocketconnectresult.md) on `asyncBlock`. That function retrieves a [WebSocketCompletionResult](https://docs.xbox.com/reference/live/httpclient/httpclient/structs/websocketcompletionresult.md) by calling `XAsyncGetResult` with `HCWebSocketConnectAsync` as the identity. Your implementation must therefore do the following:

1. Call `XAsyncBegin` on `asyncBlock` with `HCWebSocketConnectAsync` as the identity.
2. When the connection attempt finishes, call `XAsyncComplete(asyncBlock, hr, sizeof(WebSocketCompletionResult))`.
3. In the `XAsyncOp::GetResult` operation, fill the `WebSocketCompletionResult` buffer. Set `websocket` to the `websocket` parameter, `errorCode` to the connection result, and `platformErrorCode` to a platform-specific code for diagnostics.

While the WebSocket is connected, deliver incoming messages and the close event through the functions returned by [HCWebSocketGetEventFunctions](https://docs.xbox.com/reference/live/httpclient/httpclient/functions/hcwebsocketgeteventfunctions.md).

This callback type isn't declared when `HC_NOWEBSOCKETS` is defined.

## Requirements

**Header:** httpProvider.h

**Library:** libHttpClient.lib

**Supported platforms:** Windows, XBOX One family consoles and XBOX Series consoles

## See also

[httpProvider](https://docs.xbox.com/reference/live/httpclient/httpprovider/httpprovider_members.md)\
[HCSetWebSocketFunctions](https://docs.xbox.com/reference/live/httpclient/httpprovider/functions/hcsetwebsocketfunctions.md)\
[HCGetWebSocketFunctions](https://docs.xbox.com/reference/live/httpclient/httpprovider/functions/hcgetwebsocketfunctions.md)


## Related topics

- [HCWebSocketGetPingInterval](https://docs.xbox.com/reference/live/httpclient/httpprovider/functions/hcwebsocketgetpinginterval.md)
- [HCGetWebSocketFunctions](https://docs.xbox.com/reference/live/httpclient/httpprovider/functions/hcgetwebsocketfunctions.md)
- [HCSetWebSocketFunctions](https://docs.xbox.com/reference/live/httpclient/httpprovider/functions/hcsetwebsocketfunctions.md)
- [httpProvider (contents)](https://docs.xbox.com/reference/live/httpclient/httpprovider/httpprovider_members.md)
- [HCWebSocketConnectAsync](https://docs.xbox.com/reference/live/httpclient/httpclient/functions/hcwebsocketconnectasync.md)
