Resurgence (PY2022)
Codebase for the Husky Robotics 2021-2022 rover Resurgence
Loading...
Searching...
No Matches
net::websocket::SingleClientWSServer Class Reference

A WebSocket server class that only accepts a single client at a time to each endpoint served by this server. More...

#include <WebSocketServer.h>

Public Member Functions

 SingleClientWSServer (const std::string &serverName, uint16_t port)
 Construct a new server object, without starting it.
 
 ~SingleClientWSServer ()
 Destroy the server after calling stop()
 
SingleClientWSServeroperator= (const SingleClientWSServer &)=delete
 
bool start ()
 Start the server.
 
void stop ()
 Stop the server.
 
bool addProtocol (std::unique_ptr< WebSocketProtocol > protocol)
 Register a protocol with this server.
 
void sendRawString (const std::string &protocolPath, const std::string &str)
 Send a string to the client connected to the given protocol path, if there is one.
 
void sendJSON (const std::string &protocolPath, const json &obj)
 Serialize the JSON as a string and send it to the client connected to the given protocol path, if there is one.
 

Detailed Description

A WebSocket server class that only accepts a single client at a time to each endpoint served by this server.

Users of this class will create endpoints by adding websocket protocols using addProtocol(). This server processes messages from the client and invokes message handlers, which are defined by a websocket::WebSocketProtocol object.

Messages recieved by this server are expected to be JSON-encoded. Further requirements are dictated by the websocket::WebSocketProtocol documentation. The entire json object will be passed to message handlers.

Constructor & Destructor Documentation

◆ SingleClientWSServer()

net::websocket::SingleClientWSServer::SingleClientWSServer ( const std::string & serverName,
uint16_t port )

Construct a new server object, without starting it.

Parameters
serverNameThe name of this server. This is used only for logging purposes.
portThe local port to bind to. This is not validated until start() is called.

Member Function Documentation

◆ addProtocol()

bool net::websocket::SingleClientWSServer::addProtocol ( std::unique_ptr< WebSocketProtocol > protocol)

Register a protocol with this server.

It will create an endpoint at a protocol path which clients can connect to. The protocol and message handlers are defined by the protocol object. If a protocol already exists at the given protocol path, do nothing.

Parameters
protocolThe protocol object to add to the server.
Returns
true iff no protocol existed with the given protocol path, and the given protocol was successfully added.

◆ sendJSON()

void net::websocket::SingleClientWSServer::sendJSON ( const std::string & protocolPath,
const json & obj )

Serialize the JSON as a string and send it to the client connected to the given protocol path, if there is one.

Parameters
protocolPathThe protocol path which the client is connected to.
objThe JSON object to serialize and send to the client.

◆ sendRawString()

void net::websocket::SingleClientWSServer::sendRawString ( const std::string & protocolPath,
const std::string & str )

Send a string to the client connected to the given protocol path, if there is one.

Parameters
protocolPathThe protocol path which the client is connected to.
strThe message to send to the client.

◆ start()

bool net::websocket::SingleClientWSServer::start ( )

Start the server.

Returns
true iff the server was successfully started. May return false if the server is already running.

◆ stop()

void net::websocket::SingleClientWSServer::stop ( )

Stop the server.

Disconnects from all clients and shuts down the server.


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