NAP
Public Member Functions | Public Attributes | Protected Member Functions | Protected Attributes | List of all members
IWebSocketClient Class Referenceabstract

#include <websocketclient.h>

Public Member Functions

 IWebSocketClient (WebSocketService &service)
 
virtual bool init (utility::ErrorState &errorState) override
 
virtual void onDestroy () override
 
bool isConnected () const
 
bool reconnect (utility::ErrorState &error)
 
const WebSocketConnectiongetConnection () const
 
- Public Member Functions inherited from WebSocketInterface
 WebSocketInterface (WebSocketService &service)
 
virtual ~WebSocketInterface ()
 
template<typename T >
T & as ()
 
template<typename T >
const T & as () const
 
- Public Member Functions inherited from Resource
 Resource ()
 
- Public Member Functions inherited from Object
 Object ()
 
virtual ~Object ()
 
 Object (Object &)=delete
 
Objectoperator= (const Object &)=delete
 
 Object (Object &&)=delete
 
Objectoperator= (Object &&)=delete
 

Public Attributes

ResourcePtr< IWebSocketClientEndPointmEndPoint
 Property: 'EndPoint' the client endpoint that manages all connections. More...
 
ResourcePtr< WebSocketTicketmTicket = nullptr
 Property: 'Ticket' optional identification token. More...
 
std::string mURI
 Property: "URI" Server URI to open connection to. More...
 
- Public Attributes inherited from Object
std::string mID
 Property: 'mID' unique name of the object. Used as an identifier by the system. More...
 

Protected Member Functions

virtual void onConnectionOpened ()=0
 
virtual void onConnectionClosed (int code, const std::string &reason)=0
 
virtual void onConnectionFailed (int code, const std::string &reason)=0
 
virtual void onMessageReceived (const WebSocketMessage &msg)=0
 
- Protected Member Functions inherited from WebSocketInterface
void addEvent (WebSocketEventPtr newEvent)
 

Protected Attributes

WebSocketConnection mConnection
 Web-socket connection. More...
 
- Protected Attributes inherited from WebSocketInterface
std::queue< WebSocketEventPtrmEvents
 
std::mutex mEventMutex
 
WebSocketServicemService = nullptr
 

Additional Inherited Members

- Static Public Member Functions inherited from Object
static bool isIDProperty (rtti::Instance &object, const rtti::Property &property)
 

Description

Interface for all web-socket clients. Derive from this class to implement your own web-socket client. Override the various virtual functions to receive web-socket client updates. The virtual functions are called from a different thread than the main thread. It is your responsibility to ensure thread-safety. On initialization the client registers itself with a nap::WebSocketClientEndPointBase and tries to connect to the server. When a connection is established the onConnectionOpened function is called. If the connection failed to establish the onConnectionFailed function is called. Call reconnect() to establish a new connection at run-time.

A 'ticket' can be added to every client to specify additional authorization information. This information is automatically given to the server when the client tries to establish a connection. The server can accept or reject the connection based on that information. When no ticket is provided the client will try to connect without any authorization.

Inheritance diagram for IWebSocketClient:
[legend]
Collaboration diagram for IWebSocketClient:
[legend]

Constructor & Destructor Documentation

◆ IWebSocketClient()

Constructor

Parameters
servicethe web-socket service that forwards events to the application.

Member Function Documentation

◆ getConnection()

const WebSocketConnection& getConnection ( ) const
Returns
server connection handle.

◆ init()

virtual bool init ( utility::ErrorState errorState)
overridevirtual

Registers the web-socket client interface with the endpoint. The client 'tries' to establish a connection with the server. The success of the initialization step does not depend on whether or not the client manages to connect to the service.

Parameters
errorStatecontains the error message when initialization fails
Returns
if initialization succeeded.

Reimplemented from WebSocketInterface.

◆ isConnected()

bool isConnected ( ) const
Returns
if the client is connected to the server.

◆ onConnectionClosed()

virtual void onConnectionClosed ( int  code,
const std::string &  reason 
)
protectedpure virtual

Occurs when the connection to the server is closed.

Parameters
codethe closing code
reasonthe reason for closing the connection.

Implemented in APIWebSocketClient.

◆ onConnectionFailed()

virtual void onConnectionFailed ( int  code,
const std::string &  reason 
)
protectedpure virtual

Occurs when connecting to the server fails.

Parameters
codethe closing code
reasonthe reason why the connection failed.

Implemented in APIWebSocketClient.

◆ onConnectionOpened()

virtual void onConnectionOpened ( )
protectedpure virtual

Occurs when a new connection to the server is opened.

Implemented in APIWebSocketClient.

◆ onDestroy()

virtual void onDestroy ( )
overridevirtual

Unregisters the web-socket client interface with the endpoint.

Reimplemented from Object.

◆ onMessageReceived()

virtual void onMessageReceived ( const WebSocketMessage msg)
protectedpure virtual

Occurs when a new message from the server to this client is received.

Parameters
msgthe received message

Implemented in APIWebSocketClient.

◆ reconnect()

bool reconnect ( utility::ErrorState error)

Tries to reconnect this client to the server. The return value does not mean the connection is established! It only tells you that the reconnect operation succeeded. When the connection is established the onConnectionOpened function is called. Failure to establish the connection results in the onConnectionFailed function to be called.

Parameters
errorcontains the error if the reconnect operation failed.
Returns
if the reconnect operation succeeded.

Member Data Documentation

◆ mConnection

WebSocketConnection mConnection
protected

Web-socket connection.

◆ mEndPoint

Property: 'EndPoint' the client endpoint that manages all connections.

◆ mTicket

ResourcePtr<WebSocketTicket> mTicket = nullptr

Property: 'Ticket' optional identification token.

◆ mURI

std::string mURI

Property: "URI" Server URI to open connection to.