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

Class representing a WebSocket Server. More...

#include <WebSocketServer.h>

Inheritance diagram for giri::WebSocketServer:
Collaboration diagram for giri::WebSocketServer:

Public Types

using SPtr = std::shared_ptr< WebSocketServer >
 
using UPtr = std::unique_ptr< WebSocketServer >
 
using WPtr = std::weak_ptr< WebSocketServer >
 
- 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

 WebSocketServer (const std::string &address="0.0.0.0", const std::string &port="80", bool ssl=false, const size_t numThreads=1, const std::filesystem::path &cert="", const std::filesystem::path &key="")
 
void run ()
 
WebSocketSession::SPtr getSession () const
 
bool getSSL () const
 
std::filesystem::path getCert () const
 
std::filesystem::path getKey () const
 
- Public Member Functions inherited from giri::Observable< WebSocketServer >
void subscribe (const std::weak_ptr< Observer< WebSocketServer > > &obs)
 
void unsubscribe (const std::weak_ptr< Observer< WebSocketServer > > &obs)
 
void unsubscribeAll ()
 
void notify ()
 

Detailed Description

Class representing a WebSocket Server.

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 WebSocketServerObserver :
public Observer<WebSocketServer>,
public Observer<WebSocketSession>,
public std::enable_shared_from_this<WebSocketServerObserver>
{
public:
void update(WebSocketServer::SPtr serv){
std::cout << "Connected... " << serv->getSession()->getClientIP() << ":" << serv->getSession()->getClientPort() << std::endl;
// subscribe to session to receive notifications on message
serv->getSession()->subscribe(this->shared_from_this());
}
void update(WebSocketSession::SPtr sess){
std::cout << "Echoing received message: " << sess->getMessage() << std::endl;
sess->send(sess->getMessage()); // send back message
}
using SPtr = std::shared_ptr<WebSocketServerObserver>;
using UPtr = std::unique_ptr<WebSocketServerObserver>;
using WPtr = std::weak_ptr<WebSocketServerObserver>;
};
int main()
{
WebSocketServer::SPtr srv = std::make_shared<WebSocketServer>("0.0.0.0", "1204");
WebSocketServerObserver::SPtr obs = std::make_shared<WebSocketServerObserver>();
srv->subscribe(obs);
srv->run();
while(true){}; // block until ctrl + c is pressed.
return EXIT_SUCCESS;
}
Simple Websocket Server implementation.
Namespace for giri's C++ support library.
Definition: Base64.h:47

Constructor & Destructor Documentation

◆ WebSocketServer()

giri::WebSocketServer::WebSocketServer ( const std::string &  address = "0.0.0.0",
const std::string &  port = "80",
bool  ssl = false,
const size_t  numThreads = 1,
const std::filesystem::path &  cert = "",
const std::filesystem::path &  key = "" 
)
inline

Constructor for Websocket server.

Parameters
addressAdress to bind this server to (defaults to 0.0.0.0 for any).
portPort to listen on (defaults to 80).
sslEnable or disable ssl encryption (defaults to false).
numThreadsNumber of worker threads (defaults to 1).
certIf ssl is true path to certificate *.pem file needs to be passed.
keyIf ssl is true path to private key *.pem file needs to be passed.

Member Function Documentation

◆ getCert()

std::filesystem::path giri::WebSocketServer::getCert ( ) const
inline
Returns
Path to the used certificate *.pem file.

◆ getKey()

std::filesystem::path giri::WebSocketServer::getKey ( ) const
inline
Returns
Path to the used certificate privatekey *.pem file.

◆ getSession()

WebSocketSession::SPtr giri::WebSocketServer::getSession ( ) const
inline
Returns
last created session.

◆ getSSL()

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

◆ run()

void giri::WebSocketServer::run ( )
inline

Starts receiving messages asynchrolously. Automatically creates sessions and notifies observers when accepting new connections.


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