ndmspc  v1.2.0-0.1.rc3
Ndmspc::NWsClient Class Reference

WebSocket client for asynchronous communication using libwebsockets. More...

#include <NWsClient.h>

Public Types

using OnMessageCallback = std::function< void(const std::string &)>
 Callback type for received messages.
 

Public Member Functions

 NWsClient (int maxRetries=5, int retryDelayMs=1000)
 Constructor. More...
 
 ~NWsClient ()
 Destructor. Cleans up resources and disconnects.
 
bool Connect (const std::string &uriString)
 Connect to a WebSocket server. More...
 
void Disconnect ()
 Disconnect from the WebSocket server.
 
bool Send (const std::string &message)
 Send a message to the server. More...
 
bool IsConnected () const
 Check if the client is currently connected. More...
 
void SetOnMessageCallback (OnMessageCallback callback)
 Set the callback to be invoked when a message is received. More...
 
void LwsServiceLoop ()
 Service loop for libwebsockets running in a separate thread.
 

Static Public Member Functions

static WS_URI ParseUri (const std::string &uriString)
 Parse a WebSocket URI string into its components. More...
 

Public Attributes

struct lws_context * fLwsContext
 libwebsockets context
 
struct lws * fWsi
 WebSocket instance.
 
std::thread fLwsServiceThread
 Thread running the service loop.
 
int fMaxRetries
 Maximum connection retries.
 
int fRetryDelayMs
 Delay between retries (ms)
 
std::string fHost
 Hostname.
 
int fPort
 Port number.
 
std::string fPath
 Path.
 
std::atomic< bool > fConnected
 Connection status.
 
std::atomic< bool > fShutdownRequested
 Shutdown flag.
 
std::atomic< bool > fConnectionAttemptComplete
 Connection attempt completion flag.
 
std::queue< std::string > fOutgoingMessageQueue
 Queue of outgoing messages.
 
std::mutex fOutgoingMutex
 Mutex for outgoing queue.
 
std::vector< unsigned char > fSendBuffer
 Buffer for sending messages.
 
std::condition_variable fSendCv
 Condition variable for sending messages.
 
OnMessageCallback fOnMessageCallback
 Callback for received messages.
 
std::mutex fConnectMutex
 Mutex for connection state.
 
std::condition_variable fConnectCv
 Condition variable for connection.
 

Static Public Attributes

static constexpr const char * fgProtocolName = "ndmspc-protocol"
 Protocol name for websocket communication.
 
static lws_protocols fProtocols []
 Protocols supported by libwebsockets. More...
 

Detailed Description

WebSocket client for asynchronous communication using libwebsockets.

Handles connection management, message sending/receiving, and threading.

Definition at line 49 of file NWsClient.h.

Constructor & Destructor Documentation

◆ NWsClient()

Ndmspc::NWsClient::NWsClient ( int  maxRetries = 5,
int  retryDelayMs = 1000 
)

Constructor.

Parameters
maxRetriesMaximum number of connection retries.
retryDelayMsDelay between retries in milliseconds.

Definition at line 133 of file NWsClient.cxx.

Member Function Documentation

◆ Connect()

bool Ndmspc::NWsClient::Connect ( const std::string &  uriString)

◆ IsConnected()

bool Ndmspc::NWsClient::IsConnected ( ) const
inline

Check if the client is currently connected.

Returns
True if connected.

Definition at line 88 of file NWsClient.h.

References fConnected.

◆ ParseUri()

WS_URI Ndmspc::NWsClient::ParseUri ( const std::string &  uriString)
static

Parse a WebSocket URI string into its components.

Parameters
uriStringURI string to parse.
Returns
Parsed WS_URI structure.

Definition at line 351 of file NWsClient.cxx.

References Ndmspc::WS_URI::fHost, Ndmspc::WS_URI::fPath, Ndmspc::WS_URI::fPort, and Ndmspc::WS_URI::fScheme.

Referenced by Connect().

◆ Send()

bool Ndmspc::NWsClient::Send ( const std::string &  message)

Send a message to the server.

Parameters
messageMessage string to send.
Returns
True if message was queued for sending.

Definition at line 300 of file NWsClient.cxx.

References fConnected, fOutgoingMessageQueue, fOutgoingMutex, and fWsi.

Referenced by Ndmspc::NGnNavigator::Export().

◆ SetOnMessageCallback()

void Ndmspc::NWsClient::SetOnMessageCallback ( OnMessageCallback  callback)
inline

Set the callback to be invoked when a message is received.

Parameters
callbackFunction to call with received message.

Definition at line 97 of file NWsClient.h.

References fOnMessageCallback.

Member Data Documentation

◆ fProtocols

lws_protocols Ndmspc::NWsClient::fProtocols
static
Initial value:
= {
{Ndmspc::NWsClient::fgProtocolName, lws_callback_client_impl, sizeof(Ndmspc::NWsClient *), 4096, 0, nullptr, 0},
LWS_PROTOCOL_LIST_TERM
}
WebSocket client for asynchronous communication using libwebsockets.
Definition: NWsClient.h:49
static constexpr const char * fgProtocolName
Protocol name for websocket communication.
Definition: NWsClient.h:51

Protocols supported by libwebsockets.

Definition at line 128 of file NWsClient.h.

Referenced by Connect().


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