Resurgence (PY2022)
Codebase for the Husky Robotics 2021-2022 rover Resurgence
|
Defines a protocol which will be served at an endpoint of a server. More...
#include <WebSocketProtocol.h>
Public Member Functions | |
WebSocketProtocol (const std::string &protocolPath) | |
Construct a new WebSocket protocol. | |
bool | addMessageHandler (const std::string &messageType, const msghandler_t &callback) |
Add a message handler for the given message type, if no handler already exists. | |
bool | addMessageHandler (const std::string &messageType, const msghandler_t &callback, const validator_t &validator) |
Add a message handler for the given message type, if no handler already exists. | |
bool | removeMessageHandler (const std::string &messageType) |
Remove the message handler for the given message type, if it exists. | |
bool | hasMessageHandler (const std::string &messageType) const |
Check if the given message type has an associated message handler. | |
void | addConnectionHandler (const connhandler_t &handler) |
void | addDisconnectionHandler (const connhandler_t &handler) |
void | setHeartbeatTimedOutHandler (std::chrono::milliseconds timeout, const heartbeattimeouthandler_t &handler) |
Set the handler that's called when the heartbeat times out. | |
std::string | getProtocolPath () const |
Get the protocol path of the endpoint this protocol is served on. | |
Friends | |
class | SingleClientWSServer |
Defines a protocol which will be served at an endpoint of a server.
This protocol defines several message types, and processes messages of those types, dispatching events to message handlers wherever necessary.
JSON messages must define a "type" key, which controls how messages are handled. Additionally, JSON messages must (obviously) conform the expectations of the message handlers. This can be verified at runtime by using the optional validation functionality.
net::websocket::WebSocketProtocol::WebSocketProtocol | ( | const std::string & | protocolPath | ) |
Construct a new WebSocket protocol.
This object doesn't itself "do" anything, but users should register this object with a server, which will then handle dispatching events.
protocolPath | The resource path that this protocol should be served on. This should be of the form "/foo/bar", for example. |
bool net::websocket::WebSocketProtocol::addMessageHandler | ( | const std::string & | messageType, |
const msghandler_t & | callback ) |
Add a message handler for the given message type, if no handler already exists.
Messages for this message type will not be validated. If a handler already exists, this method no-ops.
messageType | The name of the message type this handler is designed for. |
callback | The callback function that will accept the json object. |
bool net::websocket::WebSocketProtocol::addMessageHandler | ( | const std::string & | messageType, |
const msghandler_t & | callback, | ||
const validator_t & | validator ) |
Add a message handler for the given message type, if no handler already exists.
Messages for this message type will be validated using the given validator. The message handler is invoked iff the validator returns true for the json object. If a handler already exists, this method no-ops.
messageType | The name of the message type this handler is designed for. |
callback | The callback function that will accept the json object. |
validator | The validator function that validates the json object. Returns true iff the message is valid. |
std::string net::websocket::WebSocketProtocol::getProtocolPath | ( | ) | const |
Get the protocol path of the endpoint this protocol is served on.
bool net::websocket::WebSocketProtocol::hasMessageHandler | ( | const std::string & | messageType | ) | const |
Check if the given message type has an associated message handler.
messageType | The name of the message to check for. |
bool net::websocket::WebSocketProtocol::removeMessageHandler | ( | const std::string & | messageType | ) |
Remove the message handler for the given message type, if it exists.
messageType | The name of the message type to remove the handler for. |
void net::websocket::WebSocketProtocol::setHeartbeatTimedOutHandler | ( | std::chrono::milliseconds | timeout, |
const heartbeattimeouthandler_t & | handler ) |
Set the handler that's called when the heartbeat times out.
If the heartbeat is reestablished after timing out, and then times out again, this handler will be called again.
timeout | The duration after which the heartbeat times out. |
handler | The handler to call when timed out. |