![]() |
Kurlyk
|
Public facade for managing WebSocket connections, events, and message sending. More...
#include <WebSocketClient.hpp>
Public Member Functions | |
| WebSocketClient () | |
| Initializes the WebSocket client facade and its backend-specific implementation. | |
| WebSocketClient (std::unique_ptr< WebSocketConfig > config, std::function< void(bool)> callback=nullptr) | |
| Initializes the facade with a WebSocket configuration object. | |
| 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) | |
| Initializes the facade with URL-based configuration values. | |
| WebSocketClient (const WebSocketClient &)=delete | |
| WebSocketClient & | operator= (const WebSocketClient &)=delete |
| virtual | ~WebSocketClient () |
| Schedules backend shutdown through NetworkWorker before the facade is destroyed. | |
| 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. | |
| SubmitResult | submit_message (const std::string &message, long rate_limit_id=0, std::function< void(const std::error_code &)> callback=nullptr) |
| Attempts to submit a message through the WebSocket. | |
| 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. | |
| SubmitResult | submit_close (const int status=1000, const std::string &reason=std::string(), std::function< void(const std::error_code &)> callback=nullptr) |
| Attempts to submit a close request to the WebSocket server. | |
| 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_max_send_queue_size (std::size_t max_send_queue_size) |
| Sets the maximum number of outbound send operations queued for this client. | |
| 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 | |
| selected_backend_client_ptr | m_client |
| Backend-specific WebSocket client selected by compile-time platform macros. | |
| std::unique_ptr< WebSocketConfig > | m_config |
| Deferred WebSocket configuration copied into the backend before connect(). | |
Public facade for managing WebSocket connections, events, and message sending.
Internally owns a backend-specific WebSocket client selected at compile time for the active platform.
Definition at line 15 of file WebSocketClient.hpp.
|
inline |
Initializes the WebSocket client facade and its backend-specific implementation.
Definition at line 19 of file WebSocketClient.hpp.
|
inline |
Initializes the facade with a WebSocket configuration object.
| config | A unique pointer to a WebSocketConfig object. |
| callback | Callback invoked when configuration is completed. |
Definition at line 30 of file WebSocketClient.hpp.
|
inline |
Initializes the facade with URL-based configuration values.
| 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 50 of file WebSocketClient.hpp.
|
delete |
|
inlinevirtual |
Schedules backend shutdown through NetworkWorker before the facade is destroyed.
Definition at line 84 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 442 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 450 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 458 of file WebSocketClient.hpp.
|
inline |
Asynchronously connects to the WebSocket server.
Definition at line 126 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 146 of file WebSocketClient.hpp.
|
inline |
Connects to the WebSocket server, blocking until the connection completes.
Definition at line 161 of file WebSocketClient.hpp.
|
inline |
Asynchronously disconnects from the WebSocket server.
Definition at line 168 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 187 of file WebSocketClient.hpp.
|
inline |
Disconnects from the WebSocket server, blocking until the disconnection completes.
Definition at line 180 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 480 of file WebSocketClient.hpp.
|
inline |
Accessor for the event handler function.
Definition at line 99 of file WebSocketClient.hpp.
|
inline |
Retrieves the headers associated with the WebSocket connection.
Definition at line 266 of file WebSocketClient.hpp.
|
inline |
Retrieves the HTTP version used in the WebSocket connection.
Definition at line 260 of file WebSocketClient.hpp.
|
inline |
Retrieves the remote endpoint information.
Definition at line 272 of file WebSocketClient.hpp.
|
inlineprivate |
Initializes the WebSocket configuration if it is not already set.
Definition at line 468 of file WebSocketClient.hpp.
|
inline |
Checks if the WebSocket is connected.
Definition at line 194 of file WebSocketClient.hpp.
|
inline |
Sets a callback for WebSocket events.
| callback | The function to be executed on each WebSocket event. |
Definition at line 93 of file WebSocketClient.hpp.
|
delete |
|
inline |
Retrieves a single WebSocket event, if available.
Definition at line 254 of file WebSocketClient.hpp.
|
inline |
Retrieves all pending WebSocket events in a batch.
Definition at line 248 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 239 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 215 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 298 of file WebSocketClient.hpp.
|
inline |
Sets the Accept-Language header.
| accept_language | Accept-Language string. |
Definition at line 378 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 421 of file WebSocketClient.hpp.
|
inline |
Sets the path to the CA certificate file.
| ca_file | Path to the CA certificate file. |
Definition at line 413 of file WebSocketClient.hpp.
|
inline |
Asynchronously sets the WebSocket configuration.
| config | A unique pointer to the WebSocket configuration object. |
Definition at line 106 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 119 of file WebSocketClient.hpp.
|
inline |
Sets the cookie data.
| cookie | Cookie data string. |
Definition at line 385 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 392 of file WebSocketClient.hpp.
|
inline |
Sets the maximum number of outbound send operations queued for this client.
| max_send_queue_size | Queue limit, or 0 to keep the send queue unbounded. |
Definition at line 406 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 321 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 307 of file WebSocketClient.hpp.
|
inline |
Sets the proxy authentication credentials.
| auth | Proxy authentication in <username:password> format. |
Definition at line 340 of file WebSocketClient.hpp.
|
inline |
Configures proxy authentication credentials.
| username | Proxy username. |
| password | Proxy password. |
Definition at line 355 of file WebSocketClient.hpp.
|
inline |
Sets the proxy server address.
| server | Proxy address in <ip:port> format. |
Definition at line 333 of file WebSocketClient.hpp.
|
inline |
Sets the proxy type.
| type | Type of proxy. |
Definition at line 347 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 364 of file WebSocketClient.hpp.
|
inline |
Sets the timeout for WebSocket requests.
| request_timeout | Request timeout in seconds (0 means no timeout). |
Definition at line 399 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 280 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 288 of file WebSocketClient.hpp.
|
inline |
Sets the User-Agent header.
| user_agent | User-Agent string. |
Definition at line 371 of file WebSocketClient.hpp.
|
inline |
Sets whether to verify the server’s certificate.
| verify_cert | If true, enables server certificate verification. |
Definition at line 428 of file WebSocketClient.hpp.
|
inline |
Attempts to submit 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 227 of file WebSocketClient.hpp.
|
inline |
Attempts to submit 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 203 of file WebSocketClient.hpp.
|
private |
Backend-specific WebSocket client selected by compile-time platform macros.
Definition at line 464 of file WebSocketClient.hpp.
|
private |
Deferred WebSocket configuration copied into the backend before connect().
Definition at line 465 of file WebSocketClient.hpp.