yaze 0.3.2
Link to the Past ROM Editor
 
Loading...
Searching...
No Matches
yaze::net::HttpLibWebSocket Class Reference

Native WebSocket implementation using HTTP fallback. More...

#include <httplib_websocket.h>

Inheritance diagram for yaze::net::HttpLibWebSocket:
Collaboration diagram for yaze::net::HttpLibWebSocket:

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
 

Detailed Description

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.

Constructor & Destructor Documentation

◆ HttpLibWebSocket()

yaze::net::HttpLibWebSocket::HttpLibWebSocket ( )

Definition at line 16 of file httplib_websocket.cc.

References yaze::net::kDisconnected, and yaze::net::IWebSocket::state_.

◆ ~HttpLibWebSocket()

yaze::net::HttpLibWebSocket::~HttpLibWebSocket ( )
override

Definition at line 20 of file httplib_websocket.cc.

References Close(), yaze::net::kDisconnected, and yaze::net::IWebSocket::state_.

Here is the call graph for this function:

Member Function Documentation

◆ Connect()

absl::Status yaze::net::HttpLibWebSocket::Connect ( const std::string & url)
overridevirtual

Connect to a WebSocket server.

Parameters
urlThe WebSocket URL (ws:// or wss://)
Returns
Status indicating success or failure

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_.

Here is the call graph for this function:

◆ Send()

absl::Status yaze::net::HttpLibWebSocket::Send ( const std::string & message)
overridevirtual

Send a text message.

Parameters
messageThe text message to send
Returns
Status indicating success or failure

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_.

◆ SendBinary()

absl::Status yaze::net::HttpLibWebSocket::SendBinary ( const uint8_t * data,
size_t length )
overridevirtual

Send a binary message.

Parameters
dataThe binary data to send
lengthThe length of the data
Returns
Status indicating success or failure

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_.

◆ Close()

absl::Status yaze::net::HttpLibWebSocket::Close ( int code = 1000,
const std::string & reason = "" )
overridevirtual

Close the WebSocket connection.

Parameters
codeOptional close code
reasonOptional close reason
Returns
Status indicating success or failure

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().

Here is the call graph for this function:

◆ GetState()

WebSocketState yaze::net::HttpLibWebSocket::GetState ( ) const
inlineoverridevirtual

Get the current connection state.

Returns
Current WebSocket state

Implements yaze::net::IWebSocket.

Definition at line 67 of file httplib_websocket.h.

References yaze::net::IWebSocket::state_.

◆ OnMessage()

void yaze::net::HttpLibWebSocket::OnMessage ( MessageCallback callback)
inlineoverridevirtual

Set callback for text message events.

Parameters
callbackFunction 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_.

◆ OnBinaryMessage()

void yaze::net::HttpLibWebSocket::OnBinaryMessage ( BinaryMessageCallback callback)
inlineoverridevirtual

Set callback for binary message events.

Parameters
callbackFunction 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_.

◆ OnOpen()

void yaze::net::HttpLibWebSocket::OnOpen ( OpenCallback callback)
inlineoverridevirtual

Set callback for connection open events.

Parameters
callbackFunction 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_.

◆ OnClose()

void yaze::net::HttpLibWebSocket::OnClose ( CloseCallback callback)
inlineoverridevirtual

Set callback for connection close events.

Parameters
callbackFunction 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_.

◆ OnError()

void yaze::net::HttpLibWebSocket::OnError ( ErrorCallback callback)
inlineoverridevirtual

Set callback for error events.

Parameters
callbackFunction 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_.

◆ ParseWebSocketUrl()

absl::Status yaze::net::HttpLibWebSocket::ParseWebSocketUrl ( const std::string & ws_url,
std::string & http_url )
private

Parse WebSocket URL into HTTP components.

Parameters
ws_urlWebSocket URL (ws:// or wss://)
http_urlOutput: Converted HTTP URL
Returns
Status indicating success or failure

Definition at line 26 of file httplib_websocket.cc.

References yaze::net::IWebSocket::url_.

Referenced by Connect().

◆ ReceiveLoop()

void yaze::net::HttpLibWebSocket::ReceiveLoop ( )
private

◆ StopReceiveLoop()

void yaze::net::HttpLibWebSocket::StopReceiveLoop ( )
private

Stop the receive loop.

Definition at line 265 of file httplib_websocket.cc.

References receive_thread_, and stop_receive_.

Referenced by Close().

Member Data Documentation

◆ client_

std::shared_ptr<httplib::Client> yaze::net::HttpLibWebSocket::client_
private

Definition at line 130 of file httplib_websocket.h.

Referenced by Close(), Connect(), ReceiveLoop(), Send(), and SendBinary().

◆ receive_thread_

std::thread yaze::net::HttpLibWebSocket::receive_thread_
private

Definition at line 133 of file httplib_websocket.h.

Referenced by Connect(), and StopReceiveLoop().

◆ stop_receive_

std::atomic<bool> yaze::net::HttpLibWebSocket::stop_receive_
private

Definition at line 134 of file httplib_websocket.h.

Referenced by Connect(), ReceiveLoop(), and StopReceiveLoop().

◆ session_id_

std::string yaze::net::HttpLibWebSocket::session_id_
private

Definition at line 137 of file httplib_websocket.h.

Referenced by Close(), Connect(), ReceiveLoop(), Send(), and SendBinary().

◆ http_endpoint_

std::string yaze::net::HttpLibWebSocket::http_endpoint_
private

Definition at line 138 of file httplib_websocket.h.

Referenced by Connect().


The documentation for this class was generated from the following files: