NAP
Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
WebSocketClientEndPointSetup< config > Class Template Reference

#include <websocketclientendpoint.h>

Public Member Functions

virtual bool init (utility::ErrorState &errorState) override
 
virtual void stop () override
 
bool start (nap::utility::ErrorState &error) override
 
bool send (const WebSocketConnection &connection, const std::string &message, EWebSocketOPCode code, nap::utility::ErrorState &error) override
 
bool send (const WebSocketConnection &connection, void const *payload, int length, EWebSocketOPCode code, nap::utility::ErrorState &error) override
 
- Public Member Functions inherited from Resource
 Resource ()
 
- Public Member Functions inherited from Object
 Object ()
 
virtual ~Object ()
 
virtual void onDestroy ()
 
 Object (Object &)=delete
 
Objectoperator= (const Object &)=delete
 
 Object (Object &&)=delete
 
Objectoperator= (Object &&)=delete
 

Protected Member Functions

void run ()
 
bool registerClient (IWebSocketClient &client, utility::ErrorState &error) override
 
void unregisterClient (const IWebSocketClient &client) override
 

Protected Attributes

uint32 mLogLevel = 0
 Converted library log level. More...
 
uint32 mAccessLogLevel = 0
 Log client / server connection data. More...
 
bool mRunning = false
 If the client connection to the server is open. More...
 
std::future< void > mClientTask
 The client server thread. More...
 
std::vector< std::unique_ptr< WebSocketClientWrapper< config > > > mClients
 All unique client connections. More...
 
websocketpp::client< config > mEndPoint
 websocketpp client end point More...
 

Additional Inherited Members

- Static Public Member Functions inherited from Object
static bool isIDProperty (rtti::Instance &object, const rtti::Property &property)
 
- Public Attributes inherited from IWebSocketClientEndPoint
bool mLogConnectionUpdates = true
 Property: "LogConnectionUpdates" if client / server connection information is logged to the console. More...
 
EWebSocketLogLevel mLibraryLogLevel = EWebSocketLogLevel::Warning
 Property: "LibraryLogLevel" library related equal to or higher than requested are logged. More...
 
- Public Attributes inherited from Object
std::string mID
 Property: 'mID' unique name of the object. Used as an identifier by the system. More...
 

Description

template<typename config>
class nap::WebSocketClientEndPointSetup< config >

Web socket client endpoint implementation using a specific configuration, where config is the websocketpp (secured / unsecured) end-point configuration type.

Manages a list of client-server connections and acts as the main portal for the client to the server. Every web-socket client receives connection updates and messages from an endpoint. The endpoint is a device that can be started and stopped.

When stopped all active client-server connections are closed. This occurs when file changes are detected and the content of the application is hot-reloaded. Typically an application has only one endpoint. Multiple clients can reference the same endpoint. Every connection update and message is forwarded to the client from a background thread. To receive connection updates and messages a client must be dervied from nap::IWebSocketClient.

Note that depending on your operating system you might have to run the application as administrator

Inheritance diagram for WebSocketClientEndPointSetup< config >:
[legend]
Collaboration diagram for WebSocketClientEndPointSetup< config >:
[legend]

Member Function Documentation

◆ init()

bool init ( utility::ErrorState errorState)
overridevirtual

Initialize this object after de-serialization

Parameters
errorStatecontains the error message when initialization fails.
Returns
if the endpoint initialized correctly.

Reimplemented from Object.

◆ registerClient()

bool registerClient ( IWebSocketClient client,
utility::ErrorState error 
)
overrideprotectedvirtual

Connects a nap client to a server. The new connection is managed by this endpoint. The client is added to the list of internally managed clients.

Parameters
clientthe client to register
errorcontains the error is registration fails.
Returns
if the client was registered successfully

Implements IWebSocketClientEndPoint.

◆ run()

void run
protected

Runs the endpoint in a background thread until stopped.

◆ send() [1/2]

bool send ( const WebSocketConnection connection,
const std::string &  message,
EWebSocketOPCode  code,
nap::utility::ErrorState error 
)
overridevirtual

Sends a message to a server.

Parameters
connectionthe client connection to the server.
messagethe message to send.
codetype of message.
errorcontains the error if sending fails.
Returns
if message was sent successfully

Implements IWebSocketClientEndPoint.

◆ send() [2/2]

bool send ( const WebSocketConnection connection,
void const *  payload,
int  length,
EWebSocketOPCode  code,
nap::utility::ErrorState error 
)
overridevirtual

Sends a message using the given payload and opcode to a server.

Parameters
connectionthe client connection to the server.
payloadthe message buffer.
lengthbuffer size in bytes.
codetype of message.
errorcontains the error if sending fails.
Returns
if message was sent successfully.

Implements IWebSocketClientEndPoint.

◆ start()

bool start ( nap::utility::ErrorState error)
overridevirtual

Starts the endpoint. This is a non-blocking call. New connections are accepted. Connection updates and messages are received in a background thread.

Parameters
errorcontains the error if the endpoint can't be started.
Returns
if the endpoint started.

Reimplemented from Device.

◆ stop()

void stop
overridevirtual

Stops the endpoint. All active connections are closed.

Reimplemented from Device.

◆ unregisterClient()

void unregisterClient ( const IWebSocketClient client)
overrideprotectedvirtual

Removes a client (resource) from the list of actively managed connection. If the client connection is currently open it will be closed. Asserts if the client isn't part of the system or can't be removed.

Parameters
clientthe client to remove.

Implements IWebSocketClientEndPoint.

Member Data Documentation

◆ mAccessLogLevel

uint32 mAccessLogLevel = 0
protected

Log client / server connection data.

◆ mClients

std::vector<std::unique_ptr<WebSocketClientWrapper<config> > > mClients
protected

All unique client connections.

◆ mClientTask

std::future<void> mClientTask
protected

The client server thread.

◆ mEndPoint

websocketpp::client<config> mEndPoint
protected

websocketpp client end point

◆ mLogLevel

uint32 mLogLevel = 0
protected

Converted library log level.

◆ mRunning

bool mRunning = false
protected

If the client connection to the server is open.