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

#include <websocketserver.h>

Public Member Functions

 WebSocketServer (WebSocketService &service)
 
bool send (const WebSocketConnection &connection, const WebSocketMessage &message, nap::utility::ErrorState &error)
 
bool send (const WebSocketConnection &connection, void const *payload, int length, EWebSocketOPCode code, nap::utility::ErrorState &error)
 
bool send (const WebSocketConnection &connection, const std::string &message, EWebSocketOPCode code, nap::utility::ErrorState &error)
 
bool broadcast (const WebSocketMessage &message, nap::utility::ErrorState &error)
 
bool broadcast (void const *payload, int length, EWebSocketOPCode code, nap::utility::ErrorState &error)
 
- Public Member Functions inherited from IWebSocketServer
 IWebSocketServer (WebSocketService &service)
 
virtual bool init (utility::ErrorState &errorState) override
 
virtual void onDestroy () override
 
- 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 IWebSocketServer
ResourcePtr< IWebSocketServerEndPointmEndPoint
 Property: 'EndPoint' the server endpoint that manages all client connections. 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 WebSocketInterface
std::queue< WebSocketEventPtrmEvents
 
std::mutex mEventMutex
 
WebSocketServicemService = nullptr
 

Description

Default implementation of a web-socket server. Receives and responds to client messages over a web socket and can be used to send a reply. Implements the IWebSocketServer interface. The server converts raw messages and connection updates from a nap::WebSocketServerEndPoint into web-socket events that can be interpreted by the running application. Events are generated on a background thread and consumed on the main thread on update(). Use a nap::WebSocketComponent to receive and react to client web-socket events in your application.

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

Constructor & Destructor Documentation

◆ WebSocketServer()

Constructor

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

Member Function Documentation

◆ broadcast() [1/2]

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

Broadcasts a message to all connected clients

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

◆ broadcast() [2/2]

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

Broadcasts a message to all connected clients

Parameters
payloadthe data to send
lengthpayload byte size
codemessage type
errorcontains the error if sending fails
Returns
if message was broadcast successfully

◆ send() [1/3]

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

Sends a message with the given opcode to a client.

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

◆ send() [2/3]

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

Sends a message to a client.

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

◆ send() [3/3]

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

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

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