Resurgence (PY2022)
Codebase for the Husky Robotics 2021-2022 rover Resurgence
|
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() | |
SingleClientWSServer & | operator= (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. | |
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.
net::websocket::SingleClientWSServer::SingleClientWSServer | ( | const std::string & | serverName, |
uint16_t | port ) |
Construct a new server object, without starting it.
serverName | The name of this server. This is used only for logging purposes. |
port | The local port to bind to. This is not validated until start() is called. |
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.
protocol | The protocol object to add to the server. |
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.
protocolPath | The protocol path which the client is connected to. |
obj | The JSON object to serialize and send to the client. |
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.
protocolPath | The protocol path which the client is connected to. |
str | The message to send to the client. |
bool net::websocket::SingleClientWSServer::start | ( | ) |
Start the server.
void net::websocket::SingleClientWSServer::stop | ( | ) |
Stop the server.
Disconnects from all clients and shuts down the server.