1515#include < kcenon/messaging/serialization/message_serializer.h>
1616#include < kcenon/network/facade/websocket_facade.h>
1717#include < kcenon/network/interfaces/i_protocol_client.h>
18- #include < kcenon/network/interfaces/i_websocket_client.h>
1918#include < kcenon/network/interfaces/connection_observer.h>
2019
2120#include < atomic>
@@ -60,17 +59,13 @@ class websocket_transport::impl {
6059 , serializer_()
6160 , reconnect_attempts_(0 )
6261 , current_reconnect_delay_(config.reconnect_delay) {
63- // Create WebSocket client via facade
62+ // Create WebSocket client via facade (returns i_protocol_client)
6463 network::facade::websocket_facade facade;
6564 client_ = facade.create_client ({
6665 .client_id = generate_client_id (),
6766 .ping_interval = config.ping_interval
6867 });
6968
70- // Try to get WebSocket-specific interface
71- ws_client_ = dynamic_cast <network::interfaces::i_websocket_client*>(
72- client_.get ());
73-
7469 setup_callbacks ();
7570 }
7671
@@ -97,10 +92,8 @@ class websocket_transport::impl {
9792 state_ = transport_state::connecting;
9893 notify_state_change (transport_state::connecting);
9994
100- // Use WebSocket-specific start with path if available
101- auto result = ws_client_
102- ? ws_client_->start (config_.host , config_.port , config_.path )
103- : client_->start (config_.host , config_.port );
95+ // Connect via unified protocol client interface
96+ auto result = client_->start (config_.host , config_.port );
10497
10598 if (result.is_err ()) {
10699 state_ = transport_state::error;
@@ -125,11 +118,7 @@ class websocket_transport::impl {
125118 // Cancel any pending reconnection
126119 reconnect_attempts_ = config_.max_retries + 1 ;
127120
128- if (ws_client_) {
129- [[maybe_unused]] auto _ = ws_client_->stop ();
130- } else {
131- [[maybe_unused]] auto _ = client_->stop ();
132- }
121+ [[maybe_unused]] auto _ = client_->stop ();
133122
134123 state_ = transport_state::disconnected;
135124 notify_state_change (transport_state::disconnected);
@@ -138,10 +127,6 @@ class websocket_transport::impl {
138127 }
139128
140129 bool is_connected () const {
141- if (ws_client_) {
142- return state_ == transport_state::connected
143- && ws_client_->is_connected ();
144- }
145130 return state_ == transport_state::connected
146131 && client_->is_connected ();
147132 }
@@ -163,9 +148,7 @@ class websocket_transport::impl {
163148 }
164149
165150 auto & data = serialized.value ();
166- auto result = ws_client_
167- ? ws_client_->send_binary (std::move (data))
168- : client_->send (std::move (data));
151+ auto result = client_->send (std::move (data));
169152
170153 if (result.is_err ()) {
171154 ++stats_.errors ;
@@ -184,9 +167,7 @@ class websocket_transport::impl {
184167 }
185168
186169 std::vector<uint8_t > data_copy = data;
187- auto result = ws_client_
188- ? ws_client_->send_binary (std::move (data_copy))
189- : client_->send (std::move (data_copy));
170+ auto result = client_->send (std::move (data_copy));
190171
191172 if (result.is_err ()) {
192173 ++stats_.errors ;
@@ -298,23 +279,13 @@ class websocket_transport::impl {
298279 make_typed_error_code (messaging_error_category::not_connected));
299280 }
300281
301- if (ws_client_) {
302- std::string text_copy = text;
303- auto result = ws_client_->send_text (std::move (text_copy));
304- if (result.is_err ()) {
305- ++stats_.errors ;
306- return VoidResult::err (
307- make_typed_error_code (messaging_error_category::publication_failed));
308- }
309- } else {
310- // Fallback: send as binary via unified interface
311- std::vector<uint8_t > data (text.begin (), text.end ());
312- auto result = client_->send (std::move (data));
313- if (result.is_err ()) {
314- ++stats_.errors ;
315- return VoidResult::err (
316- make_typed_error_code (messaging_error_category::publication_failed));
317- }
282+ // Send as binary via unified protocol client interface
283+ std::vector<uint8_t > data (text.begin (), text.end ());
284+ auto result = client_->send (std::move (data));
285+ if (result.is_err ()) {
286+ ++stats_.errors ;
287+ return VoidResult::err (
288+ make_typed_error_code (messaging_error_category::publication_failed));
318289 }
319290
320291 stats_.bytes_sent += text.size ();
@@ -327,14 +298,8 @@ class websocket_transport::impl {
327298 make_typed_error_code (messaging_error_category::not_connected));
328299 }
329300
330- if (ws_client_) {
331- auto result = ws_client_->ping ();
332- if (result.is_err ()) {
333- return VoidResult::err (
334- make_typed_error_code (messaging_error_category::publication_failed));
335- }
336- }
337-
301+ // Ping is handled internally by the WebSocket facade's keepalive
302+ // mechanism (configured via ping_interval in client_config)
338303 return ok ();
339304 }
340305
@@ -355,46 +320,20 @@ class websocket_transport::impl {
355320 }
356321
357322 void setup_callbacks () {
358- if (ws_client_) {
359- // Use WebSocket-specific callbacks for richer event handling
360- ws_client_->set_connected_callback ([this ]() {
361- on_connected ();
362- });
363-
364- ws_client_->set_disconnected_callback (
365- [this ](uint16_t code, std::string_view reason) {
366- on_disconnected (code, std::string (reason));
367- });
368-
369- ws_client_->set_binary_callback (
370- [this ](const std::vector<uint8_t >& data) {
371- on_binary_message (data);
372- });
373-
374- ws_client_->set_text_callback (
375- [this ](const std::string& text) {
376- on_text_message (text);
377- });
378-
379- ws_client_->set_error_callback ([this ](std::error_code ec) {
380- on_error (ec);
381- });
382- } else {
383- // Use unified protocol client observer
384- auto observer = std::make_shared<network::interfaces::callback_adapter>();
385- observer->on_connected ([this ]() {
386- on_connected ();
387- }).on_disconnected ([this ](std::optional<std::string_view> reason) {
388- on_disconnected (1000 , reason.has_value ()
389- ? std::string (*reason) : std::string{});
390- }).on_receive ([this ](std::span<const uint8_t > data) {
391- std::vector<uint8_t > vec (data.begin (), data.end ());
392- on_binary_message (vec);
393- }).on_error ([this ](std::error_code ec) {
394- on_error (ec);
395- });
396- client_->set_observer (observer);
397- }
323+ // Use unified protocol client observer pattern
324+ auto observer = std::make_shared<network::interfaces::callback_adapter>();
325+ observer->on_connected ([this ]() {
326+ on_connected ();
327+ }).on_disconnected ([this ](std::optional<std::string_view> reason) {
328+ on_disconnected (1000 , reason.has_value ()
329+ ? std::string (*reason) : std::string{});
330+ }).on_receive ([this ](std::span<const uint8_t > data) {
331+ std::vector<uint8_t > vec (data.begin (), data.end ());
332+ on_binary_message (vec);
333+ }).on_error ([this ](std::error_code ec) {
334+ on_error (ec);
335+ });
336+ client_->set_observer (observer);
398337 }
399338
400339 void on_connected () {
@@ -507,9 +446,7 @@ class websocket_transport::impl {
507446
508447 notify_state_change (transport_state::connecting);
509448
510- auto result = ws_client_
511- ? ws_client_->start (config_.host , config_.port , config_.path )
512- : client_->start (config_.host , config_.port );
449+ auto result = client_->start (config_.host , config_.port );
513450
514451 if (result.is_err ()) {
515452 {
@@ -584,7 +521,6 @@ class websocket_transport::impl {
584521 websocket_transport_config config_;
585522 std::atomic<transport_state> state_;
586523 std::shared_ptr<network::interfaces::i_protocol_client> client_;
587- network::interfaces::i_websocket_client* ws_client_ = nullptr ;
588524 serialization::message_serializer serializer_;
589525
590526 mutable std::mutex mutex_;
0 commit comments