![]() |
Kurlyk
|
Provides an interface for managing WebSocket connections, including configuration, event handling, and message sending. More...
#include <WebSocketClient.hpp>
Public Member Functions | |
WebSocketClient () | |
Default constructor initializes the WebSocketClient. | |
WebSocketClient (std::unique_ptr< WebSocketConfig > config, std::function< void(bool)> callback=nullptr) | |
Constructor with configuration. | |
WebSocketClient (const std::string &url, const Headers &headers=Headers(), const std::string &proxy_server=std::string(), const std::string &proxy_auth=std::string(), ProxyType proxy_type=ProxyType::PROXY_HTTP, long request_timeout=20, bool reconnect=true, bool verify_cert=true, const std::string &ca_file=std::string(), int rpm=200) | |
Constructor with URL for configuration. | |
WebSocketClient (const WebSocketClient &)=delete | |
WebSocketClient & | operator= (const WebSocketClient &)=delete |
virtual | ~WebSocketClient () |
Destructor resets the WebSocket client instance. | |
void | on_event (std::function< void(std::unique_ptr< WebSocketEventData >)> callback) |
Sets a callback for WebSocket events. | |
std::function< void(std::unique_ptr< WebSocketEventData >)> & | event_handler () |
Accessor for the event handler function. | |
std::future< bool > | set_config (std::unique_ptr< WebSocketConfig > config) |
Asynchronously sets the WebSocket configuration. | |
void | set_config (std::unique_ptr< WebSocketConfig > config, std::function< void(bool)> callback) |
Sets the WebSocket configuration and executes a callback upon completion. | |
std::future< bool > | connect () |
Asynchronously connects to the WebSocket server. | |
void | connect (std::function< void(bool)> callback) |
Connects to the WebSocket server and executes a callback upon completion. | |
bool | connect_and_wait () |
Connects to the WebSocket server, blocking until the connection completes. | |
std::future< bool > | disconnect () |
Asynchronously disconnects from the WebSocket server. | |
bool | disconnect_and_wait () |
Disconnects from the WebSocket server, blocking until the disconnection completes. | |
void | disconnect (std::function< void(bool success)> callback) |
Disconnects from the WebSocket server and invokes a callback upon completion. | |
const bool | is_connected () const |
Checks if the WebSocket is connected. | |
bool | send_message (const std::string &message, long rate_limit_id=0, std::function< void(const std::error_code &)> callback=nullptr) |
Sends a message through the WebSocket. | |
bool | send_close (const int status=1000, const std::string &reason=std::string(), std::function< void(const std::error_code &)> callback=nullptr) |
Sends a close request to the WebSocket server. | |
std::list< std::unique_ptr< WebSocketEventData > > | receive_events () |
Retrieves all pending WebSocket events in a batch. | |
std::unique_ptr< WebSocketEventData > | receive_event () |
Retrieves a single WebSocket event, if available. | |
std::string | get_http_version () const |
Retrieves the HTTP version used in the WebSocket connection. | |
Headers | get_headers () const |
Retrieves the headers associated with the WebSocket connection. | |
std::string | get_remote_endpoint () const |
Retrieves the remote endpoint information. | |
void | set_url (const std::string &host, const std::string &path, const std::string &query="") |
Sets the WebSocket server URL with optional query parameters. | |
void | set_url (const std::string &url, const QueryParams &query) |
Sets the WebSocket server URL with specified query parameters. | |
void | set_accept_encoding (bool identity=false, bool deflate=false, bool gzip=false, bool brotli=false) |
Sets the Accept-Encoding header with specified encodings. | |
void | set_proxy (const std::string &ip, int port, ProxyType type=ProxyType::PROXY_HTTP) |
Sets the proxy server address. | |
void | set_proxy (const std::string &ip, int port, const std::string &username, const std::string &password, ProxyType type=ProxyType::PROXY_HTTP) |
Sets the proxy server address with authentication. | |
void | set_proxy_server (const std::string &server) |
Sets the proxy server address. | |
void | set_proxy_auth (const std::string &auth) |
Sets the proxy authentication credentials. | |
void | set_proxy_type (ProxyType type) |
Sets the proxy type. | |
void | set_proxy_auth (const std::string &username, const std::string &password) |
Configures proxy authentication credentials. | |
void | set_reconnect (bool reconnect, long reconnect_attempts=0, long reconnect_delay=0) |
Configures reconnection behavior. | |
void | set_user_agent (const std::string &user_agent) |
Sets the User-Agent header. | |
void | set_accept_language (const std::string &accept_language) |
Sets the Accept-Language header. | |
void | set_cookie (const std::string &cookie) |
Sets the cookie data. | |
void | set_idle_timeout (long idle_timeout) |
Configures the idle timeout for the WebSocket connection. | |
void | set_request_timeout (long request_timeout) |
Sets the timeout for WebSocket requests. | |
void | set_ca_file (const std::string &ca_file) |
Sets the path to the CA certificate file. | |
void | set_ca_file (bool verify_cert, const std::string &ca_file) |
Sets certificate verification and sets the CA certificate file. | |
void | set_verify_cert (bool verify_cert) |
Sets whether to verify the server’s certificate. | |
long | add_rate_limit (long requests_per_period, long period_ms) |
Adds a rate limit configuration to control the frequency of WebSocket messages. | |
long | add_rate_limit_rpm (long requests_per_minute) |
Adds a rate limit based on Requests Per Minute (RPM). | |
long | add_rate_limit_rps (long requests_per_second) |
Adds a rate limit based on Requests Per Second (RPS). | |
Private Member Functions | |
void | init_config () |
Initializes the WebSocket configuration if it is not already set. | |
Static Private Member Functions | |
static void | ensure_initialized () |
Ensures the WebSocket and network components are initialized. | |
Private Attributes | |
std::shared_ptr< IWebSocketClient > | m_client |
Pointer to the WebSocket client instance. | |
std::unique_ptr< WebSocketConfig > | m_config |
WebSocket configuration object. | |
Provides an interface for managing WebSocket connections, including configuration, event handling, and message sending.
Definition at line 12 of file WebSocketClient.hpp.
|
inline |
Default constructor initializes the WebSocketClient.
Definition at line 16 of file WebSocketClient.hpp.
|
inline |
Constructor with configuration.
config | A unique pointer to a WebSocketConfig object. |
callback | Callback invoked when configuration is completed. |
Definition at line 27 of file WebSocketClient.hpp.
|
inline |
Constructor with URL for configuration.
url | The WebSocket server URL. |
headers | HTTP headers included in the WebSocket connection request. |
proxy_server | Proxy address in <ip:port> format. |
proxy_auth | Proxy authentication in <username:password> format. |
proxy_type | Proxy type (e.g., HTTP, SOCKS5). |
request_timeout | Timeout for WebSocket requests in seconds (0 means no timeout). |
reconnect | Enables automatic reconnection if true. |
verify_cert | If true, verifies the server’s certificate and hostname according to RFC 2818. |
ca_file | Path to the Root CA certificate file. |
rpm | Requests per minute (RPM). |
Definition at line 47 of file WebSocketClient.hpp.
|
delete |
|
inlinevirtual |
Destructor resets the WebSocket client instance.
Definition at line 81 of file WebSocketClient.hpp.
|
inline |
Adds a rate limit configuration to control the frequency of WebSocket messages.
The first rate limit added will serve as the primary rate limit, applying to all WebSocket requests by default. Additional rate limits can be added for specific types of requests or contexts as needed.
requests_per_period | The maximum number of messages allowed within the specified period. |
period_ms | The time period in milliseconds during which the request limit applies. |
Definition at line 408 of file WebSocketClient.hpp.
|
inline |
Adds a rate limit based on Requests Per Minute (RPM).
requests_per_minute | Maximum number of requests allowed per minute. |
Definition at line 416 of file WebSocketClient.hpp.
|
inline |
Adds a rate limit based on Requests Per Second (RPS).
requests_per_second | Maximum number of requests allowed per second. |
Definition at line 424 of file WebSocketClient.hpp.
|
inline |
Asynchronously connects to the WebSocket server.
Definition at line 123 of file WebSocketClient.hpp.
|
inline |
Connects to the WebSocket server and executes a callback upon completion.
callback | The callback function to execute after connection, receiving a success status. |
Definition at line 143 of file WebSocketClient.hpp.
|
inline |
Connects to the WebSocket server, blocking until the connection completes.
Definition at line 158 of file WebSocketClient.hpp.
|
inline |
Asynchronously disconnects from the WebSocket server.
Definition at line 165 of file WebSocketClient.hpp.
|
inline |
Disconnects from the WebSocket server and invokes a callback upon completion.
callback | The callback function to execute after disconnection, receiving a success status. |
Definition at line 184 of file WebSocketClient.hpp.
|
inline |
Disconnects from the WebSocket server, blocking until the disconnection completes.
Definition at line 177 of file WebSocketClient.hpp.
|
inlinestaticprivate |
Ensures the WebSocket and network components are initialized.
This method is called only once per application run.
Definition at line 446 of file WebSocketClient.hpp.
|
inline |
Accessor for the event handler function.
Definition at line 96 of file WebSocketClient.hpp.
|
inline |
Retrieves the headers associated with the WebSocket connection.
Definition at line 239 of file WebSocketClient.hpp.
|
inline |
Retrieves the HTTP version used in the WebSocket connection.
Definition at line 233 of file WebSocketClient.hpp.
|
inline |
Retrieves the remote endpoint information.
Definition at line 245 of file WebSocketClient.hpp.
|
inlineprivate |
Initializes the WebSocket configuration if it is not already set.
Definition at line 434 of file WebSocketClient.hpp.
|
inline |
Checks if the WebSocket is connected.
Definition at line 191 of file WebSocketClient.hpp.
|
inline |
Sets a callback for WebSocket events.
callback | The function to be executed on each WebSocket event. |
Definition at line 90 of file WebSocketClient.hpp.
|
delete |
|
inline |
Retrieves a single WebSocket event, if available.
Definition at line 227 of file WebSocketClient.hpp.
|
inline |
Retrieves all pending WebSocket events in a batch.
Definition at line 221 of file WebSocketClient.hpp.
|
inline |
Sends a close request to the WebSocket server.
status | The status code for the close request (default: 1000). |
reason | Optional reason for closing the connection. |
callback | Optional callback to execute after sending the close request. |
Definition at line 212 of file WebSocketClient.hpp.
|
inline |
Sends a message through the WebSocket.
message | The content of the message to be sent. |
rate_limit_id | The ID of the rate limit to apply to this message. A value of 0 indicates the default or no rate limit. |
callback | An optional callback to execute after sending the message. |
Definition at line 200 of file WebSocketClient.hpp.
|
inline |
Sets the Accept-Encoding header with specified encodings.
identity | Enables identity encoding. |
deflate | Enables deflate encoding. |
gzip | Enables gzip encoding. |
brotli | Enables brotli encoding. |
Definition at line 271 of file WebSocketClient.hpp.
|
inline |
Sets the Accept-Language header.
accept_language | Accept-Language string. |
Definition at line 351 of file WebSocketClient.hpp.
|
inline |
Sets certificate verification and sets the CA certificate file.
verify_cert | If true, enables server certificate verification. |
ca_file | Path to the CA certificate file. |
Definition at line 387 of file WebSocketClient.hpp.
|
inline |
Sets the path to the CA certificate file.
ca_file | Path to the CA certificate file. |
Definition at line 379 of file WebSocketClient.hpp.
|
inline |
Asynchronously sets the WebSocket configuration.
config | A unique pointer to the WebSocket configuration object. |
Definition at line 103 of file WebSocketClient.hpp.
|
inline |
Sets the WebSocket configuration and executes a callback upon completion.
config | A unique pointer to the WebSocket configuration object. |
callback | The callback function to execute after setting the configuration, receiving a success status. |
Definition at line 116 of file WebSocketClient.hpp.
|
inline |
Sets the cookie data.
cookie | Cookie data string. |
Definition at line 358 of file WebSocketClient.hpp.
|
inline |
Configures the idle timeout for the WebSocket connection.
idle_timeout | Idle timeout in seconds (0 means no timeout). |
Definition at line 365 of file WebSocketClient.hpp.
|
inline |
Sets the proxy server address with authentication.
ip | Proxy server IP address. |
port | Proxy server port. |
username | Proxy username. |
password | Proxy password. |
type | Type of proxy (default is HTTP). |
Definition at line 294 of file WebSocketClient.hpp.
|
inline |
Sets the proxy server address.
ip | Proxy server IP address. |
port | Proxy server port. |
type | Type of proxy (default is HTTP). |
Definition at line 280 of file WebSocketClient.hpp.
|
inline |
Sets the proxy authentication credentials.
auth | Proxy authentication in <username:password> format. |
Definition at line 313 of file WebSocketClient.hpp.
|
inline |
Configures proxy authentication credentials.
username | Proxy username. |
password | Proxy password. |
Definition at line 328 of file WebSocketClient.hpp.
|
inline |
Sets the proxy server address.
server | Proxy address in <ip:port> format. |
Definition at line 306 of file WebSocketClient.hpp.
|
inline |
Sets the proxy type.
type | Type of proxy. |
Definition at line 320 of file WebSocketClient.hpp.
|
inline |
Configures reconnection behavior.
reconnect | Enables automatic reconnection. |
reconnect_attempts | Number of reconnection attempts (0 means infinite attempts). |
reconnect_delay | Delay in seconds between reconnection attempts. |
Definition at line 337 of file WebSocketClient.hpp.
|
inline |
Sets the timeout for WebSocket requests.
request_timeout | Request timeout in seconds (0 means no timeout). |
Definition at line 372 of file WebSocketClient.hpp.
|
inline |
Sets the WebSocket server URL with optional query parameters.
host | Hostname or IP address. |
path | Path for the request. |
query | Optional query parameters. |
Definition at line 253 of file WebSocketClient.hpp.
|
inline |
Sets the WebSocket server URL with specified query parameters.
url | Full URL of the server. |
query | Query parameters as a dictionary. |
Definition at line 261 of file WebSocketClient.hpp.
|
inline |
Sets the User-Agent header.
user_agent | User-Agent string. |
Definition at line 344 of file WebSocketClient.hpp.
|
inline |
Sets whether to verify the server’s certificate.
verify_cert | If true, enables server certificate verification. |
Definition at line 394 of file WebSocketClient.hpp.
|
private |
Pointer to the WebSocket client instance.
Definition at line 430 of file WebSocketClient.hpp.
|
private |
WebSocket configuration object.
Definition at line 431 of file WebSocketClient.hpp.