|
ndmspc
v1.2.0-0.1.rc3
|
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... | |
WebSocket client for asynchronous communication using libwebsockets.
Handles connection management, message sending/receiving, and threading.
Definition at line 49 of file NWsClient.h.
| Ndmspc::NWsClient::NWsClient | ( | int | maxRetries = 5, |
| int | retryDelayMs = 1000 |
||
| ) |
Constructor.
| maxRetries | Maximum number of connection retries. |
| retryDelayMs | Delay between retries in milliseconds. |
Definition at line 133 of file NWsClient.cxx.
| bool Ndmspc::NWsClient::Connect | ( | const std::string & | uriString | ) |
Connect to a WebSocket server.
| uriString | URI string to connect to. |
Definition at line 148 of file NWsClient.cxx.
References Disconnect(), fConnectCv, fConnected, fConnectionAttemptComplete, fConnectMutex, fgProtocolName, Ndmspc::WS_URI::fHost, fHost, fLwsContext, fLwsServiceThread, fMaxRetries, Ndmspc::WS_URI::fPath, fPath, Ndmspc::WS_URI::fPort, fPort, fProtocols, fRetryDelayMs, Ndmspc::WS_URI::fScheme, fShutdownRequested, fWsi, LwsServiceLoop(), and ParseUri().
Referenced by Ndmspc::NGnNavigator::Export().
|
inline |
Check if the client is currently connected.
Definition at line 88 of file NWsClient.h.
References fConnected.
|
static |
Parse a WebSocket URI string into its components.
| uriString | URI string to parse. |
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().
| bool Ndmspc::NWsClient::Send | ( | const std::string & | message | ) |
Send a message to the server.
| message | Message string to send. |
Definition at line 300 of file NWsClient.cxx.
References fConnected, fOutgoingMessageQueue, fOutgoingMutex, and fWsi.
Referenced by Ndmspc::NGnNavigator::Export().
|
inline |
Set the callback to be invoked when a message is received.
| callback | Function to call with received message. |
Definition at line 97 of file NWsClient.h.
References fOnMessageCallback.
|
static |
Protocols supported by libwebsockets.
Definition at line 128 of file NWsClient.h.
Referenced by Connect().