Giri's C++ Support Library
C++ library providing everything you need to quickly create awesome applications.
giri::WebSocketClient Class Reference

Simple Websocket Client. More...

#include <WebSocketClient.h>

Inheritance diagram for giri::WebSocketClient:
Collaboration diagram for giri::WebSocketClient:

Public Types

using SPtr = std::shared_ptr< WebSocketClient >
 
using UPtr = std::unique_ptr< WebSocketClient >
 
- Public Types inherited from giri::Object< T >
using SPtr = std::shared_ptr< T >
 
using UPtr = std::unique_ptr< T >
 
using WPtr = std::weak_ptr< T >
 

Public Member Functions

 WebSocketClient (const std::string &host, const std::string &port, bool ssl=false, const size_t numThreads=1, const std::string &resource="/")
 
void send (const std::string &msg)
 msg Message to send More...
 
std::string getMessage ()
 
std::string getHost ()
 
std::string getPort ()
 
bool getSSL ()
 
boost::system::error_code getError ()
 
void run ()
 
std::string receive ()
 
void close ()
 
- Public Member Functions inherited from giri::Observable< WebSocketClient >
void subscribe (const std::weak_ptr< Observer< WebSocketClient > > &obs)
 
void unsubscribe (const std::weak_ptr< Observer< WebSocketClient > > &obs)
 
void unsubscribeAll ()
 
void notify ()
 

Detailed Description

Simple Websocket Client.

Supporting synchronous and asynchronous read.

Example Usage:

#include <iostream>
#include <string>
using namespace giri;
// observer to receive async answers.
// use std::lock_guard + std::mutex if resources of this class are
// accessed by multiple threads
class WSCObserver : public Observer<WebSocketClient>
{
protected:
void update(WebSocketClient::SPtr ptr)
{
std::cout << "async receive: " << ptr->getMessage() << std::endl;
}
};
int main() {
// Create client and async observer
WebSocketClient::SPtr wsc = std::make_shared<WebSocketClient>("echo.websocket.org", "80");
WSCObserver::SPtr obs = std::make_shared<WSCObserver>();
// -- asynchronous write and read --
std::cout << "Message to send (async):" << std::endl;
std::string msg;
std::cin >> msg;
wsc->run();
wsc->subscribe(obs);
wsc->send(msg); // obs will receive answer
// -- synchronous write and read --
std::cout << "Message to send (sync):" << std::endl;
std::cin >> msg;
wsc->send(msg); // Send message
std::cout << "sync receive: " << wsc->receive() << std::endl; // blocking wait for answer
return EXIT_SUCCESS;
}
Simple Websocket Client implementation.
Namespace for giri's C++ support library.
Definition: Base64.h:47

Constructor & Destructor Documentation

◆ WebSocketClient()

giri::WebSocketClient::WebSocketClient ( const std::string &  host,
const std::string &  port,
bool  ssl = false,
const size_t  numThreads = 1,
const std::string &  resource = "/" 
)
inline

Constructor to create a websocket client.

Parameters
hostWebsocket server host.
portWebsocket server port.
sslEnables or disables ssl (defaults to false).
numThreadsNumber of threads to be used (defaults to 1).
resourceResource where websocket server is bound to (defaults to "/").

Member Function Documentation

◆ close()

void giri::WebSocketClient::close ( )
inline

Close websocket client.

◆ getError()

boost::system::error_code giri::WebSocketClient::getError ( )
inline
Returns
Error code if last request was not successful.

◆ getHost()

std::string giri::WebSocketClient::getHost ( )
inline
Returns
Host this client is connected to.

◆ getMessage()

std::string giri::WebSocketClient::getMessage ( )
inline
Returns
last received message.

◆ getPort()

std::string giri::WebSocketClient::getPort ( )
inline
Returns
Port this client is connected to.

◆ getSSL()

bool giri::WebSocketClient::getSSL ( )
inline
Returns
true if ssl is enabled, false otherwise.

◆ receive()

std::string giri::WebSocketClient::receive ( )
inline

Receives message synchronously. Blocks until a message was received.

Returns
received message.

◆ run()

void giri::WebSocketClient::run ( )
inline

Starts receiving messages asynchrolously. Automatically notifies subscribed Observer objects on new messages.

◆ send()

void giri::WebSocketClient::send ( const std::string &  msg)
inline

msg Message to send

Send a message to server. Blocks until message was sent.


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