KEEL 1.0.0
Minimal C11 HTTP client/server library built on epoll/kqueue/io_uring/poll
Loading...
Searching...
No Matches
Data Structures | Macros | Typedefs | Functions
websocket_client.h File Reference

WebSocket client API. More...

#include <keel/allocator.h>
#include <keel/event_ctx.h>
#include <keel/tls.h>
#include <keel/websocket.h>
Include dependency graph for websocket_client.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Data Structures

struct  KlWsClientConfig
 
struct  KlWsClientCallbacks
 

Macros

#define KL_WS_CLIENT_DEFAULT_TIMEOUT_MS   30000
 Default connect/handshake timeout in milliseconds.
 
#define KL_WS_CLIENT_RECV_BUF_SIZE   8192
 Receive buffer size in bytes.
 
#define KL_WS_CLIENT_DEFAULT_MAX_FRAME   (16 * 1024 * 1024)
 Default maximum frame size (16 MB).
 
#define KL_WS_CLIENT_KEY_B64_SIZE   25
 Base64-encoded key size (16 bytes -> 24 Base64 chars + NUL).
 
#define KL_WS_CLIENT_UPGRADE_BUF_INIT   512
 Initial upgrade response buffer size.
 

Typedefs

typedef struct KlWsClientConn KlWsClientConn
 

Functions

KlWsClientConnkl_ws_client_connect (KlEventCtx *ev, KlAllocator *alloc, const KlWsClientConfig *cfg, const char *url, const KlWsClientCallbacks *cbs, void *user_data)
 Connect to a WebSocket server.
 
int kl_ws_client_send_text (KlWsClientConn *ws, const char *data, size_t len)
 Send a text frame.
 
int kl_ws_client_send_binary (KlWsClientConn *ws, const char *data, size_t len)
 Send a binary frame.
 
int kl_ws_client_send_ping (KlWsClientConn *ws, const char *data, size_t len)
 Send a ping frame.
 
void kl_ws_client_close (KlWsClientConn *ws, uint16_t code, const char *reason, size_t reason_len)
 Initiate close handshake.
 
void kl_ws_client_free (KlWsClientConn *ws)
 Free all WebSocket client resources.
 

Detailed Description

WebSocket client API.

Async WebSocket client driven by KlEventCtx watchers. Reuses the shared frame parser from websocket.h. Client frames are masked per RFC 6455 Section 5.3.

Macro Definition Documentation

◆ KL_WS_CLIENT_DEFAULT_TIMEOUT_MS

#define KL_WS_CLIENT_DEFAULT_TIMEOUT_MS   30000

Default connect/handshake timeout in milliseconds.

◆ KL_WS_CLIENT_RECV_BUF_SIZE

#define KL_WS_CLIENT_RECV_BUF_SIZE   8192

Receive buffer size in bytes.

◆ KL_WS_CLIENT_DEFAULT_MAX_FRAME

#define KL_WS_CLIENT_DEFAULT_MAX_FRAME   (16 * 1024 * 1024)

Default maximum frame size (16 MB).

◆ KL_WS_CLIENT_KEY_B64_SIZE

#define KL_WS_CLIENT_KEY_B64_SIZE   25

Base64-encoded key size (16 bytes -> 24 Base64 chars + NUL).

◆ KL_WS_CLIENT_UPGRADE_BUF_INIT

#define KL_WS_CLIENT_UPGRADE_BUF_INIT   512

Initial upgrade response buffer size.

Typedef Documentation

◆ KlWsClientConn

Function Documentation

◆ kl_ws_client_connect()

KlWsClientConn * kl_ws_client_connect ( KlEventCtx ev,
KlAllocator alloc,
const KlWsClientConfig cfg,
const char *  url,
const KlWsClientCallbacks cbs,
void *  user_data 
)

Connect to a WebSocket server.

Initiates a non-blocking TCP connect, optional TLS handshake, and WebSocket HTTP upgrade. Callbacks fire on the event loop thread.

Parameters
evEvent context (standalone or &server->ev).
allocAllocator (must outlive the connection).
cfgConfiguration (NULL for defaults, timeout/TLS/limits).
urlWebSocket URL (ws:// or wss://).
cbsCallbacks (on_open, on_message, on_close, on_error).
user_dataOpaque pointer passed to all callbacks.
Returns
Connection handle, or NULL on immediate failure.

◆ kl_ws_client_send_text()

int kl_ws_client_send_text ( KlWsClientConn ws,
const char *  data,
size_t  len 
)

Send a text frame.

Returns
0 on success, -1 on error.

◆ kl_ws_client_send_binary()

int kl_ws_client_send_binary ( KlWsClientConn ws,
const char *  data,
size_t  len 
)

Send a binary frame.

Returns
0 on success, -1 on error.

◆ kl_ws_client_send_ping()

int kl_ws_client_send_ping ( KlWsClientConn ws,
const char *  data,
size_t  len 
)

Send a ping frame.

Returns
0 on success, -1 on error.

◆ kl_ws_client_close()

void kl_ws_client_close ( KlWsClientConn ws,
uint16_t  code,
const char *  reason,
size_t  reason_len 
)

Initiate close handshake.

◆ kl_ws_client_free()

void kl_ws_client_free ( KlWsClientConn ws)

Free all WebSocket client resources.