# ws::WebSocketAdapter

<a id="websocketadapter-1"></a>

# WebSocketAdapter

```cpp
#include <websocket.h>

class WebSocketAdapter
```

Defined in src/http/include/icy/http/websocket.h:267

> **Inherits:** `SocketEmitter`
> **Subclassed by:** [`ConnectionAdapter`](https://sourcey.com/examples/docs-showcase/reference/cpp/icy-http-ws-ConnectionAdapter.md#connectionadapter), [`WebSocket`](https://sourcey.com/examples/docs-showcase/reference/cpp/icy-http-ws-WebSocket.md#websocket)

[WebSocket](https://sourcey.com/examples/docs-showcase/reference/cpp/icy-http-ws-WebSocket.md#websocket) protocol adapter for both client and server endpoints.

## Friends

| Name | Description |
|------|-------------|
| [`WebSocketFramer`](#websocketframer-3)  |  |

---

<a id="websocketframer-3"></a>

### WebSocketFramer

```cpp
friend class WebSocketFramer
```

Defined in src/http/include/icy/http/websocket.h:350

## Public Attributes

| Return | Name | Description |
|--------|------|-------------|
| `net::Socket::Ptr` | [`socket`](#socket)  | Pointer to the underlying socket. Sent data will be proxied to this socket. |

---

<a id="socket"></a>

### socket

```cpp
net::Socket::Ptr socket
```

Defined in src/http/include/icy/http/websocket.h:304

Pointer to the underlying socket. Sent data will be proxied to this socket.

## Public Methods

| Return | Name | Description |
|--------|------|-------------|
|  | [`WebSocketAdapter`](#websocketadapter-2)  | Creates a [WebSocketAdapter](#websocketadapter-1) using the given socket, mode and HTTP message objects. |
| `ssize_t` | [`send`](#send) `virtual` `override` | Frames and sends data to the peer's address. |
| `ssize_t` | [`send`](#send-1) `virtual` `override` | Frames and sends data to a specific peer address (for UDP-backed sockets). |
| `ssize_t` | [`sendOwned`](#sendowned) `virtual` `override` |  |
| `ssize_t` | [`sendOwned`](#sendowned-1) `virtual` `override` |  |
| `bool` | [`shutdown`](#shutdown) `virtual` | Sends a [WebSocket](https://sourcey.com/examples/docs-showcase/reference/cpp/icy-http-ws-WebSocket.md#websocket) CLOSE frame with the given status code and message, then closes the underlying socket. |
| `void` | [`sendClientRequest`](#sendclientrequest) `virtual` | Client side. |
| `void` | [`handleClientResponse`](#handleclientresponse) `virtual` | Parses the server's HTTP upgrade response and completes the handshake. Any data remaining in the buffer after the HTTP response is re-fed as [WebSocket](https://sourcey.com/examples/docs-showcase/reference/cpp/icy-http-ws-WebSocket.md#websocket) frames. |
| `void` | [`handleServerRequest`](#handleserverrequest) `virtual` | Server side. |
| `void` | [`onHandshakeComplete`](#onhandshakecomplete) `virtual` | Called when the [WebSocket](https://sourcey.com/examples/docs-showcase/reference/cpp/icy-http-ws-WebSocket.md#websocket) handshake completes. Emits the connect event to downstream handlers. |

---

<a id="websocketadapter-2"></a>

### WebSocketAdapter

```cpp
WebSocketAdapter(const net::Socket::Ptr & socket, ws::Mode mode, http::Request & request, http::Response & response)
```

Defined in src/http/include/icy/http/websocket.h:275

Creates a [WebSocketAdapter](#websocketadapter-1) using the given socket, mode and HTTP message objects.

#### Parameters

| Parameter | Type | Description |
|-----------|------|-------------|
| `socket` | `const net::Socket::Ptr &` | The underlying TCP or SSL socket. |
| `mode` | [`ws::Mode`](api_Mode.md#namespaceicy_1_1http_1_1ws_1aaa1718b12afc4665a0b4a088bdfe6db5) | ServerSide or ClientSide. |
| `request` | `http::Request &` | HTTP request used for the handshake. |
| `response` | `http::Response &` | HTTP response used for the handshake. |

---

<a id="send"></a>

### send

`virtual` `override`

```cpp
virtual ssize_t send(const char * data, size_t len, int flags = 0) override
```

Defined in src/http/include/icy/http/websocket.h:283

Frames and sends data to the peer's address. 
#### Returns
Number of bytes sent, or -1 on error.

#### Parameters

| Parameter | Type | Description |
|-----------|------|-------------|
| `data` | `const char *` | Pointer to the payload. |
| `len` | `size_t` | Payload length in bytes. |
| `flags` | `int` | [ws::SendFlags::Text](api_icy-http-ws.md#namespaceicy_1_1http_1_1ws_1a366b9e6ee51be48df27ea06f8d20e7a2afdf2a2c6b74320a613fe71490a96e2ae) or [ws::SendFlags::Binary](api_icy-http-ws.md#namespaceicy_1_1http_1_1ws_1a366b9e6ee51be48df27ea06f8d20e7a2a0ca55f2c2a2e576e0aa1c08a4e845ed7). |

---

<a id="send-1"></a>

### send

`virtual` `override`

```cpp
virtual ssize_t send(const char * data, size_t len, const net::Address & peerAddr, int flags = 0) override
```

Defined in src/http/include/icy/http/websocket.h:291

Frames and sends data to a specific peer address (for UDP-backed sockets). 
#### Returns
Number of bytes sent, or -1 on error.

#### Parameters

| Parameter | Type | Description |
|-----------|------|-------------|
| `data` | `const char *` | Pointer to the payload. |
| `len` | `size_t` | Payload length in bytes. |
| `peerAddr` | `const net::Address &` | Destination address. |
| `flags` | `int` | [ws::SendFlags::Text](api_icy-http-ws.md#namespaceicy_1_1http_1_1ws_1a366b9e6ee51be48df27ea06f8d20e7a2afdf2a2c6b74320a613fe71490a96e2ae) or [ws::SendFlags::Binary](api_icy-http-ws.md#namespaceicy_1_1http_1_1ws_1a366b9e6ee51be48df27ea06f8d20e7a2a0ca55f2c2a2e576e0aa1c08a4e845ed7). |

---

<a id="sendowned"></a>

### sendOwned

`virtual` `override`

```cpp
virtual ssize_t sendOwned(Buffer && buffer, int flags = 0) override
```

Defined in src/http/include/icy/http/websocket.h:292

---

<a id="sendowned-1"></a>

### sendOwned

`virtual` `override`

```cpp
virtual ssize_t sendOwned(Buffer && buffer, const net::Address & peerAddr, int flags = 0) override
```

Defined in src/http/include/icy/http/websocket.h:293

---

<a id="shutdown"></a>

### shutdown

`virtual`

```cpp
virtual bool shutdown(uint16_t statusCode, const std::string & statusMessage)
```

Defined in src/http/include/icy/http/websocket.h:300

Sends a [WebSocket](https://sourcey.com/examples/docs-showcase/reference/cpp/icy-http-ws-WebSocket.md#websocket) CLOSE frame with the given status code and message, then closes the underlying socket. 
#### Returns
true if the close frame was sent successfully.

#### Parameters

| Parameter | Type | Description |
|-----------|------|-------------|
| `statusCode` | `uint16_t` | [WebSocket](https://sourcey.com/examples/docs-showcase/reference/cpp/icy-http-ws-WebSocket.md#websocket) close status code (e.g. 1000 for normal close). |
| `statusMessage` | `const std::string &` | Human-readable reason for closing. |

---

<a id="sendclientrequest"></a>

### sendClientRequest

`virtual`

```cpp
virtual void sendClientRequest()
```

Defined in src/http/include/icy/http/websocket.h:311

Client side.

Sends the [WebSocket](https://sourcey.com/examples/docs-showcase/reference/cpp/icy-http-ws-WebSocket.md#websocket) HTTP upgrade request to initiate the handshake. Called automatically on socket connect.

---

<a id="handleclientresponse"></a>

### handleClientResponse

`virtual`

```cpp
virtual void handleClientResponse(const MutableBuffer & buffer, const net::Address & peerAddr)
```

Defined in src/http/include/icy/http/websocket.h:317

Parses the server's HTTP upgrade response and completes the handshake. Any data remaining in the buffer after the HTTP response is re-fed as [WebSocket](https://sourcey.com/examples/docs-showcase/reference/cpp/icy-http-ws-WebSocket.md#websocket) frames.

#### Parameters

| Parameter | Type | Description |
|-----------|------|-------------|
| `buffer` | const [`MutableBuffer`](https://sourcey.com/examples/docs-showcase/reference/cpp/icy-MutableBuffer.md#mutablebuffer-6) & | [Buffer](https://sourcey.com/examples/docs-showcase/reference/cpp/base.md#buffer) containing the server's HTTP response. |
| `peerAddr` | `const net::Address &` | Address of the peer. |

---

<a id="handleserverrequest"></a>

### handleServerRequest

`virtual`

```cpp
virtual void handleServerRequest(const MutableBuffer & buffer, const net::Address & peerAddr)
```

Defined in src/http/include/icy/http/websocket.h:325

Server side.

Parses the client's HTTP upgrade request and sends the 101 response.

#### Parameters

| Parameter | Type | Description |
|-----------|------|-------------|
| `buffer` | const [`MutableBuffer`](https://sourcey.com/examples/docs-showcase/reference/cpp/icy-MutableBuffer.md#mutablebuffer-6) & | [Buffer](https://sourcey.com/examples/docs-showcase/reference/cpp/base.md#buffer) containing the client's HTTP upgrade request. |
| `peerAddr` | `const net::Address &` | Address of the peer. |

---

<a id="onhandshakecomplete"></a>

### onHandshakeComplete

`virtual`

```cpp
virtual void onHandshakeComplete()
```

Defined in src/http/include/icy/http/websocket.h:339

Called when the [WebSocket](https://sourcey.com/examples/docs-showcase/reference/cpp/icy-http-ws-WebSocket.md#websocket) handshake completes. Emits the connect event to downstream handlers.

#### Reimplemented by

- [`ConnectionAdapter::onHandshakeComplete`](https://sourcey.com/examples/docs-showcase/reference/cpp/icy-http-ws-ConnectionAdapter.md#onhandshakecomplete-1)

## Protected Attributes

| Return | Name | Description |
|--------|------|-------------|
| [`WebSocketFramer`](#websocketframer-3) | [`framer`](#framer)  |  |
| `http::Request &` | [`_request`](#_request)  |  |
| `http::Response &` | [`_response`](#_response)  |  |
| [`ws::CloseState`](api_CloseState.md#namespaceicy_1_1http_1_1ws_1a9cf2405c87857c796e6170104f7f77bd) | [`_closeState`](#_closestate)  |  |

---

<a id="framer"></a>

### framer

```cpp
WebSocketFramer framer
```

Type: [`WebSocketFramer`](#websocketframer-3)

Defined in src/http/include/icy/http/websocket.h:352

---

<a id="_request"></a>

### _request

```cpp
http::Request & _request
```

Defined in src/http/include/icy/http/websocket.h:353

---

<a id="_response"></a>

### _response

```cpp
http::Response & _response
```

Defined in src/http/include/icy/http/websocket.h:354

---

<a id="_closestate"></a>

### _closeState

```cpp
ws::CloseState _closeState {ws::CloseState::Open}
```

Type: [`ws::CloseState`](api_CloseState.md#namespaceicy_1_1http_1_1ws_1a9cf2405c87857c796e6170104f7f77bd)

Defined in src/http/include/icy/http/websocket.h:355

## Protected Methods

| Return | Name | Description |
|--------|------|-------------|
|  | [`~WebSocketAdapter`](#websocketadapter-3) `virtual` |  |
| `bool` | [`sendControlFrame`](#sendcontrolframe)  |  |
| `void` | [`resetFrameState`](#resetframestate)  |  |

---

<a id="websocketadapter-3"></a>

### ~WebSocketAdapter

`virtual`

```cpp
virtual ~WebSocketAdapter()
```

Defined in src/http/include/icy/http/websocket.h:342

---

<a id="sendcontrolframe"></a>

### sendControlFrame

```cpp
bool sendControlFrame(ws::Opcode opcode, const char * payload, size_t payloadLen, const net::Address & peerAddr)
```

Defined in src/http/include/icy/http/websocket.h:344

---

<a id="resetframestate"></a>

### resetFrameState

```cpp
void resetFrameState()
```

Defined in src/http/include/icy/http/websocket.h:348

## Private Methods

| Return | Name | Description |
|--------|------|-------------|
| `bool` | [`onSocketConnect`](#onsocketconnect) `virtual` `override` | Called by the socket on connect; initiates the client handshake. |
| `bool` | [`onSocketRecv`](#onsocketrecv) `virtual` `override` | Called by the socket on each received buffer; handles handshake or frame parsing depending on state. |
| `bool` | [`onSocketClose`](#onsocketclose) `virtual` `override` | Called by the socket on close; resets framer state. |

---

<a id="onsocketconnect"></a>

### onSocketConnect

`virtual` `override`

```cpp
virtual bool onSocketConnect(net::Socket & socket) override
```

Defined in src/http/include/icy/http/websocket.h:328

Called by the socket on connect; initiates the client handshake.

---

<a id="onsocketrecv"></a>

### onSocketRecv

`virtual` `override`

```cpp
virtual bool onSocketRecv(net::Socket & socket, const MutableBuffer & buffer, const net::Address & peerAddress) override
```

Defined in src/http/include/icy/http/websocket.h:332

Called by the socket on each received buffer; handles handshake or frame parsing depending on state.

---

<a id="onsocketclose"></a>

### onSocketClose

`virtual` `override`

```cpp
virtual bool onSocketClose(net::Socket & socket) override
```

Defined in src/http/include/icy/http/websocket.h:335

Called by the socket on close; resets framer state.
