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

Defines a protocol which will be served at an endpoint of a server. More...

#include <WebSocketProtocol.h>

Inheritance diagram for net::websocket::WebSocketProtocol:

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
 

Detailed Description

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.

Constructor & Destructor Documentation

◆ WebSocketProtocol()

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.

Parameters
protocolPathThe resource path that this protocol should be served on. This should be of the form "/foo/bar", for example.

Member Function Documentation

◆ addMessageHandler() [1/2]

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.

Parameters
messageTypeThe name of the message type this handler is designed for.
callbackThe callback function that will accept the json object.
Returns
true if no handler already existed. false if a handler already exists.

◆ addMessageHandler() [2/2]

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.

Parameters
messageTypeThe name of the message type this handler is designed for.
callbackThe callback function that will accept the json object.
validatorThe validator function that validates the json object. Returns true iff the message is valid.
Returns
true if no handler already existed. false if a handler already exists.

◆ getProtocolPath()

std::string net::websocket::WebSocketProtocol::getProtocolPath ( ) const

Get the protocol path of the endpoint this protocol is served on.

Returns
The protocol path, of the form "/foo/bar".

◆ hasMessageHandler()

bool net::websocket::WebSocketProtocol::hasMessageHandler ( const std::string & messageType) const

Check if the given message type has an associated message handler.

Parameters
messageTypeThe name of the message to check for.
Returns
true if a handler exists for this message type, false otherwise.

◆ removeMessageHandler()

bool net::websocket::WebSocketProtocol::removeMessageHandler ( const std::string & messageType)

Remove the message handler for the given message type, if it exists.

Parameters
messageTypeThe name of the message type to remove the handler for.
Returns
true if a handler existed for this message type and was removed, false otherwise.

◆ setHeartbeatTimedOutHandler()

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.

Parameters
timeoutThe duration after which the heartbeat times out.
handlerThe handler to call when timed out.

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