16#include <system_error>
32 class websocket_socket;
49 std::map<std::string, std::string>
headers;
97 :
public std::enable_shared_from_this<messaging_ws_client>
150 [[nodiscard]] auto
start_client(std::string_view host, uint16_t port,
163 [[nodiscard]] auto
client_id() const -> const std::
string&;
182 [[nodiscard]]
auto is_running() const ->
bool override;
204 [[nodiscard]] auto
start(
205 std::string_view host,
207 std::string_view path = "/") ->
VoidResult override;
223 [[nodiscard]] auto
is_connected() const ->
bool override;
246 std::vector<uint8_t>&& data,
256 [[nodiscard]] auto
ping(std::vector<uint8_t>&& payload = {}) ->
VoidResult override;
266 [[nodiscard]]
auto close(
267 uint16_t code = 1000,
268 std::string_view reason =
"") ->
VoidResult override;
343 auto on_ping(
const std::vector<uint8_t>& payload) -> void;
353 auto on_error(std::error_code ec) -> void;
376 const std::string& reason) -> void;
409 std::unique_ptr<asio::executor_work_guard<asio::io_context::executor_type>>
work_guard_;
Thread-safe callback registration and invocation manager.
High-level WebSocket client with automatic connection management.
utils::lifecycle_manager lifecycle_
auto start(std::string_view host, uint16_t port, std::string_view path="/") -> VoidResult override
Starts the WebSocket client connecting to the specified endpoint.
auto set_text_callback(interfaces::i_websocket_client::text_callback_t callback) -> void override
Sets the callback for text messages (interface version).
auto do_start_impl(std::string_view host, uint16_t port, std::string_view path) -> VoidResult
WebSocket-specific implementation of client start.
auto is_running() const -> bool override
Checks if the client is currently running.
auto ping(std::vector< uint8_t > &&payload={}) -> VoidResult override
Sends a ping frame (interface version).
messaging_ws_client(std::string_view client_id)
Constructs a WebSocket client.
auto stop_client() -> VoidResult
Stops the client and releases all resources.
auto on_error(std::error_code ec) -> void
Handles errors.
std::function< void()> connected_callback_t
Callback type for connection established.
auto do_connect() -> void
Initiates async connection to the server.
auto set_connected_callback(interfaces::i_websocket_client::connected_callback_t callback) -> void override
Sets the callback for connection established (interface version).
auto on_message(const internal::ws_message &msg) -> void
Handles received WebSocket messages.
auto stop() -> VoidResult override
Stops the WebSocket client.
auto on_close(internal::ws_close_code code, const std::string &reason) -> void
Handles connection close.
auto wait_for_stop() -> void override
Blocks until stop() is called.
std::future< void > io_context_future_
auto invoke_error_callback(std::error_code ec) -> void
Invokes the error callback.
auto invoke_message_callback(const internal::ws_message &msg) -> void
Invokes the message callback.
~messaging_ws_client() noexcept override
Destructor. Automatically stops the client if still running.
std::function< void(internal::ws_close_code, const std::string &)> disconnected_callback_t
Callback type for disconnection with close code.
std::function< void(const std::string &)> text_message_callback_t
Callback type for text messages.
auto set_binary_callback(interfaces::i_websocket_client::binary_callback_t callback) -> void override
Sets the callback for binary messages (interface version).
auto send_text(std::string &&message, interfaces::i_websocket_client::send_callback_t handler=nullptr) -> VoidResult override
Sends a text message (interface version).
std::mutex ws_socket_mutex_
auto invoke_connected_callback() -> void
Invokes the connected callback.
std::atomic< bool > is_connected_
std::shared_ptr< integration::thread_pool_interface > thread_pool_
auto is_connected() const -> bool override
Checks if the WebSocket connection is established.
auto set_disconnected_callback(interfaces::i_websocket_client::disconnected_callback_t callback) -> void override
Sets the callback for disconnection (interface version).
std::function< void(const internal::ws_message &)> message_callback_t
Callback type for WebSocket messages.
auto start_client(const ws_client_config &config) -> VoidResult
Starts the client with full configuration.
auto set_error_callback(interfaces::i_websocket_client::error_callback_t callback) -> void override
Sets the callback for errors (interface version).
auto close(uint16_t code=1000, std::string_view reason="") -> VoidResult override
Closes the WebSocket connection gracefully (interface version).
auto on_ping(const std::vector< uint8_t > &payload) -> void
Handles ping frames.
auto do_stop_impl() -> VoidResult
WebSocket-specific implementation of client stop.
auto send_binary(std::vector< uint8_t > &&data, interfaces::i_websocket_client::send_callback_t handler=nullptr) -> VoidResult override
Sends a binary message (interface version).
auto invoke_disconnected_callback(internal::ws_close_code code, const std::string &reason) -> void
Invokes the disconnected callback.
std::function< void(std::error_code)> error_callback_t
Callback type for errors.
auto client_id() const -> const std::string &
Returns the client identifier.
std::shared_ptr< internal::websocket_socket > ws_socket_
std::unique_ptr< asio::executor_work_guard< asio::io_context::executor_type > > work_guard_
auto send_ping(std::vector< uint8_t > &&payload={}) -> VoidResult
Sends a ping frame.
std::function< void(const std::vector< uint8_t > &)> binary_message_callback_t
Callback type for binary messages.
std::unique_ptr< asio::io_context > io_context_
Interface for WebSocket client components.
std::function< void(std::error_code, std::size_t)> send_callback_t
Callback type for send completion.
std::function< void()> connected_callback_t
Callback type for connection established.
std::function< void(const std::vector< uint8_t > &)> binary_callback_t
Callback type for binary messages.
std::function< void(const std::string &)> text_callback_t
Callback type for text messages.
std::function< void(std::error_code)> error_callback_t
Callback type for errors.
std::function< void(uint16_t code, std::string_view reason)> disconnected_callback_t
Callback type for disconnection (with close code and reason)
Thread-safe lifecycle state management for network components.
Component lifecycle management (start, stop, restart).
ws_close_code
WebSocket close status codes (RFC 6455 Section 7.4).
Result< std::monostate > VoidResult
ws_client_callback
Callback indices for messaging_ws_client.
Network-specific error and result type definitions.
Configuration for WebSocket client.
std::string path
WebSocket path.
std::chrono::milliseconds connect_timeout
Connection timeout.
std::string host
Server hostname or IP.
bool auto_pong
Auto-respond to pings.
std::chrono::milliseconds ping_interval
Ping interval.
bool auto_reconnect
Auto-reconnect on disconnect.
uint16_t port
Server port.
std::map< std::string, std::string > headers
Additional HTTP headers.
size_t max_message_size
Max message size (10MB)
Represents a complete WebSocket message.
Thread system integration interface for network_system.