NAP
Public Member Functions | List of all members
WebSocketClient Class Reference

#include <websocketclient.h>

Public Member Functions

 WebSocketClient (WebSocketService &service)
 
bool send (const std::string &message, EWebSocketOPCode code, nap::utility::ErrorState &error)
 
bool send (void const *payload, int length, EWebSocketOPCode code, nap::utility::ErrorState &error)
 
bool send (const WebSocketMessage &message, nap::utility::ErrorState &error)
 
- Public Member Functions inherited from IWebSocketClient
 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
 

Additional Inherited Members

- Static Public Member Functions inherited from Object
static bool isIDProperty (rtti::Instance &object, const rtti::Property &property)
 
- Public Attributes inherited from IWebSocketClient
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 inherited from WebSocketInterface
void addEvent (WebSocketEventPtr newEvent)
 
- Protected Attributes inherited from IWebSocketClient
WebSocketConnection mConnection
 Web-socket connection. More...
 
- Protected Attributes inherited from WebSocketInterface
std::queue< WebSocketEventPtrmEvents
 
std::mutex mEventMutex
 
WebSocketServicemService = nullptr
 

Description

Default implementation of a web-socket client. Converts connection updates and messages into web-socket events. These events are consumed by the nap::WebSocketService on the main application thread. On update all events in the queue are forwarded to a nap::WebSocketComponent. Use a nap::WebSocketComponent to receive and react to web-socket events in your application. On initialization the client registers itself with a nap::WebSocketClientEndPointBase and tries to connect to the server. Call reconnect() to establish a new connection at run-time.

A 'ticket' can be added to specify additional authorization information (username / password). 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 WebSocketClient:
[legend]
Collaboration diagram for WebSocketClient:
[legend]

Constructor & Destructor Documentation

◆ WebSocketClient()

Constructor

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

Member Function Documentation

◆ send() [1/3]

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

Sends a message with the given opcode to the server. The message is send immediately.

Parameters
messagethe message to send
codemessage type
errorcontains the error if sending fails
Returns
if the message was sent successfully

◆ send() [2/3]

bool send ( const WebSocketMessage message,
nap::utility::ErrorState error 
)

Sends a message to the server. The message is send immediately.

Parameters
messagethe message to send
errorcontains the error if sending fails
Returns
if the message was sent successfully.

◆ send() [3/3]

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

Sends a message using the given payload and opcode to the server. The message is send immediately.

Parameters
payloadthe message buffer
lengthtotal number of bytes
codemessage type
errorcontains the error if sending fails
Returns
if the message was sent successfully