Native WebSocket implementation using HTTP fallback. More...
#include <httplib_websocket.h>


Public Member Functions | |
| HttpLibWebSocket () | |
| ~HttpLibWebSocket () override | |
| absl::Status | Connect (const std::string &url) override |
| Connect to a WebSocket server. | |
| absl::Status | Send (const std::string &message) override |
| Send a text message. | |
| absl::Status | SendBinary (const uint8_t *data, size_t length) override |
| Send a binary message. | |
| absl::Status | Close (int code=1000, const std::string &reason="") override |
| Close the WebSocket connection. | |
| WebSocketState | GetState () const override |
| Get the current connection state. | |
| void | OnMessage (MessageCallback callback) override |
| Set callback for text message events. | |
| void | OnBinaryMessage (BinaryMessageCallback callback) override |
| Set callback for binary message events. | |
| void | OnOpen (OpenCallback callback) override |
| Set callback for connection open events. | |
| void | OnClose (CloseCallback callback) override |
| Set callback for connection close events. | |
| void | OnError (ErrorCallback callback) override |
| Set callback for error events. | |
Public Member Functions inherited from yaze::net::IWebSocket | |
| virtual | ~IWebSocket ()=default |
| virtual bool | IsConnected () const |
| Check if the WebSocket is connected. | |
| virtual std::string | GetUrl () const |
| Get the WebSocket URL. | |
| virtual void | SetAutoReconnect (bool enable, int delay_seconds=5) |
| Set automatic reconnection. | |
Private Member Functions | |
| absl::Status | ParseWebSocketUrl (const std::string &ws_url, std::string &http_url) |
| Parse WebSocket URL into HTTP components. | |
| void | ReceiveLoop () |
| Background thread for receiving messages (polling) | |
| void | StopReceiveLoop () |
| Stop the receive loop. | |
Private Attributes | |
| std::shared_ptr< httplib::Client > | client_ |
| std::thread | receive_thread_ |
| std::atomic< bool > | stop_receive_ |
| std::string | session_id_ |
| std::string | http_endpoint_ |
Additional Inherited Members | |
Public Types inherited from yaze::net::IWebSocket | |
| using | MessageCallback = std::function<void(const std::string& message)> |
| using | BinaryMessageCallback = std::function<void(const uint8_t* data, size_t length)> |
| using | OpenCallback = std::function<void()> |
| using | CloseCallback = std::function<void(int code, const std::string& reason)> |
| using | ErrorCallback = std::function<void(const std::string& error)> |
Protected Attributes inherited from yaze::net::IWebSocket | |
| std::string | url_ |
| WebSocketState | state_ = WebSocketState::kDisconnected |
| MessageCallback | message_callback_ |
| BinaryMessageCallback | binary_message_callback_ |
| OpenCallback | open_callback_ |
| CloseCallback | close_callback_ |
| ErrorCallback | error_callback_ |
| bool | auto_reconnect_ = false |
| int | reconnect_delay_seconds_ = 5 |
Native WebSocket implementation using HTTP fallback.
Note: cpp-httplib doesn't have full WebSocket support, so this implementation uses HTTP long-polling as a fallback. For production use, consider integrating a proper WebSocket library like websocketpp or libwebsockets.
Definition at line 27 of file httplib_websocket.h.
| yaze::net::HttpLibWebSocket::HttpLibWebSocket | ( | ) |
Definition at line 16 of file httplib_websocket.cc.
References yaze::net::kDisconnected, and yaze::net::IWebSocket::state_.
|
override |
Definition at line 20 of file httplib_websocket.cc.
References Close(), yaze::net::kDisconnected, and yaze::net::IWebSocket::state_.

|
overridevirtual |
Connect to a WebSocket server.
| url | The WebSocket URL (ws:// or wss://) |
Implements yaze::net::IWebSocket.
Definition at line 51 of file httplib_websocket.cc.
References client_, http_endpoint_, yaze::net::kConnected, yaze::net::kConnecting, yaze::net::kDisconnected, yaze::net::kError, yaze::net::IWebSocket::open_callback_, ParseWebSocketUrl(), receive_thread_, ReceiveLoop(), RETURN_IF_ERROR, session_id_, yaze::net::IWebSocket::state_, and stop_receive_.

|
overridevirtual |
Send a text message.
| message | The text message to send |
Implements yaze::net::IWebSocket.
Definition at line 121 of file httplib_websocket.cc.
References client_, yaze::net::kConnected, session_id_, and yaze::net::IWebSocket::state_.
|
overridevirtual |
Send a binary message.
| data | The binary data to send |
| length | The length of the data |
Reimplemented from yaze::net::IWebSocket.
Definition at line 152 of file httplib_websocket.cc.
References client_, yaze::net::kConnected, session_id_, and yaze::net::IWebSocket::state_.
|
overridevirtual |
Close the WebSocket connection.
| code | Optional close code |
| reason | Optional close reason |
Implements yaze::net::IWebSocket.
Definition at line 184 of file httplib_websocket.cc.
References client_, yaze::net::IWebSocket::close_callback_, yaze::net::kClosed, yaze::net::kClosing, yaze::net::kDisconnected, session_id_, yaze::net::IWebSocket::state_, and StopReceiveLoop().
Referenced by ~HttpLibWebSocket().

|
inlineoverridevirtual |
Get the current connection state.
Implements yaze::net::IWebSocket.
Definition at line 67 of file httplib_websocket.h.
References yaze::net::IWebSocket::state_.
|
inlineoverridevirtual |
Set callback for text message events.
| callback | Function to call when a text message is received |
Implements yaze::net::IWebSocket.
Definition at line 73 of file httplib_websocket.h.
References yaze::net::IWebSocket::message_callback_.
|
inlineoverridevirtual |
Set callback for binary message events.
| callback | Function to call when binary data is received |
Reimplemented from yaze::net::IWebSocket.
Definition at line 81 of file httplib_websocket.h.
References yaze::net::IWebSocket::binary_message_callback_.
|
inlineoverridevirtual |
Set callback for connection open events.
| callback | Function to call when connection is established |
Implements yaze::net::IWebSocket.
Definition at line 89 of file httplib_websocket.h.
References yaze::net::IWebSocket::open_callback_.
|
inlineoverridevirtual |
Set callback for connection close events.
| callback | Function to call when connection is closed |
Implements yaze::net::IWebSocket.
Definition at line 97 of file httplib_websocket.h.
References yaze::net::IWebSocket::close_callback_.
|
inlineoverridevirtual |
Set callback for error events.
| callback | Function to call when an error occurs |
Implements yaze::net::IWebSocket.
Definition at line 105 of file httplib_websocket.h.
References yaze::net::IWebSocket::error_callback_.
|
private |
Parse WebSocket URL into HTTP components.
| ws_url | WebSocket URL (ws:// or wss://) |
| http_url | Output: Converted HTTP URL |
Definition at line 26 of file httplib_websocket.cc.
References yaze::net::IWebSocket::url_.
Referenced by Connect().
|
private |
Background thread for receiving messages (polling)
Definition at line 219 of file httplib_websocket.cc.
References client_, yaze::net::IWebSocket::error_callback_, yaze::net::kConnected, yaze::net::IWebSocket::message_callback_, session_id_, yaze::net::IWebSocket::state_, and stop_receive_.
Referenced by Connect().
|
private |
Stop the receive loop.
Definition at line 265 of file httplib_websocket.cc.
References receive_thread_, and stop_receive_.
Referenced by Close().
|
private |
Definition at line 130 of file httplib_websocket.h.
Referenced by Close(), Connect(), ReceiveLoop(), Send(), and SendBinary().
|
private |
Definition at line 133 of file httplib_websocket.h.
Referenced by Connect(), and StopReceiveLoop().
|
private |
Definition at line 134 of file httplib_websocket.h.
Referenced by Connect(), ReceiveLoop(), and StopReceiveLoop().
|
private |
Definition at line 137 of file httplib_websocket.h.
Referenced by Close(), Connect(), ReceiveLoop(), Send(), and SendBinary().
|
private |
Definition at line 138 of file httplib_websocket.h.
Referenced by Connect().