1 #ifndef NDMSPC_WEBSOCKET_CLIENT_H
2 #define NDMSPC_WEBSOCKET_CLIENT_H
4 #include <libwebsockets.h>
9 #include <condition_variable>
58 NWsClient(
int maxRetries = 5,
int retryDelayMs = 1000);
70 bool Connect(
const std::string & uriString);
82 bool Send(
const std::string & message);
WebSocket client for asynchronous communication using libwebsockets.
void LwsServiceLoop()
Service loop for libwebsockets running in a separate thread.
NWsClient(int maxRetries=5, int retryDelayMs=1000)
Constructor.
struct lws * fWsi
WebSocket instance.
int fRetryDelayMs
Delay between retries (ms)
bool Connect(const std::string &uriString)
Connect to a WebSocket server.
static constexpr const char * fgProtocolName
Protocol name for websocket communication.
int fMaxRetries
Maximum connection retries.
void Disconnect()
Disconnect from the WebSocket server.
static WS_URI ParseUri(const std::string &uriString)
Parse a WebSocket URI string into its components.
std::atomic< bool > fShutdownRequested
Shutdown flag.
static lws_protocols fProtocols[]
Protocols supported by libwebsockets.
std::atomic< bool > fConnectionAttemptComplete
Connection attempt completion flag.
std::condition_variable fSendCv
Condition variable for sending messages.
bool Send(const std::string &message)
Send a message to the server.
bool IsConnected() const
Check if the client is currently connected.
struct lws_context * fLwsContext
libwebsockets context
std::vector< unsigned char > fSendBuffer
Buffer for sending messages.
std::atomic< bool > fConnected
Connection status.
std::queue< std::string > fOutgoingMessageQueue
Queue of outgoing messages.
std::mutex fConnectMutex
Mutex for connection state.
std::function< void(const std::string &)> OnMessageCallback
Callback type for received messages.
std::string fHost
Hostname.
std::mutex fOutgoingMutex
Mutex for outgoing queue.
std::thread fLwsServiceThread
Thread running the service loop.
std::condition_variable fConnectCv
Condition variable for connection.
OnMessageCallback fOnMessageCallback
Callback for received messages.
~NWsClient()
Destructor. Cleans up resources and disconnects.
void SetOnMessageCallback(OnMessageCallback callback)
Set the callback to be invoked when a message is received.
Structure representing a parsed WebSocket URI.
std::string fScheme
URI scheme (e.g., "ws", "wss")
std::string fPath
Path component.
std::string fHost
Hostname or IP address.