12 std::string_view client_id,
13 std::chrono::milliseconds ping_interval)
14 : client_id_(client_id)
15 , ping_interval_(ping_interval)
16 , client_(std::make_shared<core::messaging_ws_client>(std::string(client_id)))
35 path_ = std::string(path);
51 client_->wait_for_stop();
65 "Client not initialized",
66 "ws_client_adapter::start"
71 return client_->start(host, port, path_);
80 "Client not initialized",
81 "ws_client_adapter::stop"
85 return client_->stop();
94 "Client not initialized",
95 "ws_client_adapter::send"
99 if (!client_->is_connected())
103 "Client is not connected - call start() first",
104 "ws_client_adapter::send"
109 return client_->send_binary(std::move(data));
119 std::lock_guard<std::mutex> lock(callbacks_mutex_);
120 observer_ = std::move(observer);
125 std::lock_guard<std::mutex> lock(callbacks_mutex_);
126 receive_callback_ = std::move(callback);
131 std::lock_guard<std::mutex> lock(callbacks_mutex_);
132 connected_callback_ = std::move(callback);
137 std::lock_guard<std::mutex> lock(callbacks_mutex_);
138 disconnected_callback_ = std::move(callback);
143 std::lock_guard<std::mutex> lock(callbacks_mutex_);
144 error_callback_ = std::move(callback);
159 client_->set_binary_callback(
160 [
this](
const std::vector<uint8_t>& data)
162 std::shared_ptr<interfaces::connection_observer> observer_copy;
165 std::lock_guard<std::mutex> lock(callbacks_mutex_);
166 observer_copy = observer_;
167 callback_copy = receive_callback_;
172 observer_copy->on_receive(data);
181 client_->set_connected_callback(
184 std::shared_ptr<interfaces::connection_observer> observer_copy;
187 std::lock_guard<std::mutex> lock(callbacks_mutex_);
188 observer_copy = observer_;
189 callback_copy = connected_callback_;
194 observer_copy->on_connected();
203 client_->set_disconnected_callback(
204 [
this](uint16_t , std::string_view )
206 std::shared_ptr<interfaces::connection_observer> observer_copy;
209 std::lock_guard<std::mutex> lock(callbacks_mutex_);
210 observer_copy = observer_;
211 callback_copy = disconnected_callback_;
216 observer_copy->on_disconnected();
225 client_->set_error_callback(
226 [
this](std::error_code ec)
228 std::shared_ptr<interfaces::connection_observer> observer_copy;
231 std::lock_guard<std::mutex> lock(callbacks_mutex_);
232 observer_copy = observer_;
233 callback_copy = error_callback_;
238 observer_copy->on_error(ec);
std::function< void(const std::vector< uint8_t > &)> receive_callback_t
Callback type for received data.
std::function< void()> connected_callback_t
Callback type for connection established.
std::function< void(std::error_code)> error_callback_t
Callback type for errors.
std::function< void()> disconnected_callback_t
Callback type for disconnection.
auto stop() -> VoidResult override
Stops the client and closes the connection.
auto set_path(std::string_view path) -> void
Sets the WebSocket path for connection.
auto set_disconnected_callback(disconnected_callback_t callback) -> void override
Sets the callback for disconnection.
auto wait_for_stop() -> void override
Blocks until the component has stopped.
std::shared_ptr< core::messaging_ws_client > client_
auto setup_internal_callbacks() -> void
Sets up internal callbacks to bridge WebSocket callbacks to i_protocol_client callbacks.
auto set_receive_callback(receive_callback_t callback) -> void override
Sets the callback for received data.
~ws_client_adapter() override
Destructor ensures proper cleanup.
auto start(std::string_view host, uint16_t port) -> VoidResult override
Starts the client and connects to the specified server.
auto is_running() const -> bool override
Checks if the component is currently running.
auto is_connected() const -> bool override
Checks if the client is connected to the server.
auto send(std::vector< uint8_t > &&data) -> VoidResult override
Sends data to the connected server.
auto set_observer(std::shared_ptr< interfaces::connection_observer > observer) -> void override
Sets the connection observer for unified event handling.
auto set_connected_callback(connected_callback_t callback) -> void override
Sets the callback for connection established.
auto set_error_callback(error_callback_t callback) -> void override
Sets the callback for errors.
ws_client_adapter(std::string_view client_id, std::chrono::milliseconds ping_interval=std::chrono::seconds(30))
Constructs an adapter with a unique client ID.
constexpr int invalid_argument
constexpr int internal_error
VoidResult error_void(int code, const std::string &message, const std::string &source="network_system", const std::string &details="")