Apollo  6.0
Open source self driving car software
Public Types | Public Member Functions | List of all members
apollo::dreamview::WebSocketHandler Class Reference

The WebSocketHandler, built on top of CivetWebSocketHandler, is a websocket handler that handles different types of websocket related events. More...

#include <websocket_handler.h>

Inheritance diagram for apollo::dreamview::WebSocketHandler:
Inheritance graph
Collaboration diagram for apollo::dreamview::WebSocketHandler:
Collaboration graph

Public Types

using Json = nlohmann::json
 
using Connection = struct mg_connection
 
using MessageHandler = std::function< void(const Json &, Connection *)>
 
using ConnectionReadyHandler = std::function< void(Connection *)>
 

Public Member Functions

 WebSocketHandler (const std::string &name)
 
bool handleConnection (CivetServer *server, const Connection *conn) override
 Callback method for when the client intends to establish a websocket connection, before websocket handshake. More...
 
void handleReadyState (CivetServer *server, Connection *conn) override
 Callback method for when websocket handshake is successfully completed, and connection is ready for data exchange. More...
 
bool handleData (CivetServer *server, Connection *conn, int bits, char *data, size_t data_len) override
 Callback method for when a data frame has been received from the client. More...
 
bool handleJsonData (Connection *conn, const std::string &data)
 
bool handleBinaryData (Connection *conn, const std::string &data)
 
void handleClose (CivetServer *server, const Connection *conn) override
 Callback method for when the connection is closed. More...
 
bool BroadcastData (const std::string &data, bool skippable=false)
 Sends the provided data to all the connected clients. More...
 
bool SendData (Connection *conn, const std::string &data, bool skippable=false, int op_code=MG_WEBSOCKET_OPCODE_TEXT)
 Sends the provided data to a specific connected client. More...
 
bool SendBinaryData (Connection *conn, const std::string &data, bool skippable=false)
 
void RegisterMessageHandler (std::string type, MessageHandler handler)
 Add a new message handler for a message type. More...
 
void RegisterConnectionReadyHandler (ConnectionReadyHandler handler)
 Add a new handler for new connections. More...
 

Detailed Description

The WebSocketHandler, built on top of CivetWebSocketHandler, is a websocket handler that handles different types of websocket related events.

Member Typedef Documentation

◆ Connection

◆ ConnectionReadyHandler

◆ Json

◆ MessageHandler

using apollo::dreamview::WebSocketHandler::MessageHandler = std::function<void(const Json &, Connection *)>

Constructor & Destructor Documentation

◆ WebSocketHandler()

apollo::dreamview::WebSocketHandler::WebSocketHandler ( const std::string &  name)
inlineexplicit

Member Function Documentation

◆ BroadcastData()

bool apollo::dreamview::WebSocketHandler::BroadcastData ( const std::string &  data,
bool  skippable = false 
)

Sends the provided data to all the connected clients.

Parameters
dataThe message string to be sent.

◆ handleBinaryData()

bool apollo::dreamview::WebSocketHandler::handleBinaryData ( Connection conn,
const std::string &  data 
)

◆ handleClose()

void apollo::dreamview::WebSocketHandler::handleClose ( CivetServer *  server,
const Connection conn 
)
override

Callback method for when the connection is closed.

Parameters
serverthe calling server
connthe connection information

◆ handleConnection()

bool apollo::dreamview::WebSocketHandler::handleConnection ( CivetServer *  server,
const Connection conn 
)
inlineoverride

Callback method for when the client intends to establish a websocket connection, before websocket handshake.

Parameters
serverthe calling server
connthe connection information
Returns
true to keep socket open, false to close it

◆ handleData()

bool apollo::dreamview::WebSocketHandler::handleData ( CivetServer *  server,
Connection conn,
int  bits,
char *  data,
size_t  data_len 
)
override

Callback method for when a data frame has been received from the client.

In the websocket protocol, data is transmitted using a sequence of frames, and each frame received invokes this callback method. Since the type of opcode (text, binary, etc) is given in the first frame, this method stores the opcode in a thread_local variable named current_opcode_. And data from each frame is accumulated to data_ until the final fragment is detected. See websocket RFC at http://tools.ietf.org/html/rfc6455, section 5.4 for more protocol and fragmentation details.

Parameters
serverthe calling server
connthe connection information
bitsfirst byte of the websocket frame, see websocket RFC at http://tools.ietf.org/html/rfc6455, section 5.2
datapayload, with mask (if any) already applied.
data_lenlength of data
Returns
true to keep socket open, false to close it

◆ handleJsonData()

bool apollo::dreamview::WebSocketHandler::handleJsonData ( Connection conn,
const std::string &  data 
)

◆ handleReadyState()

void apollo::dreamview::WebSocketHandler::handleReadyState ( CivetServer *  server,
Connection conn 
)
override

Callback method for when websocket handshake is successfully completed, and connection is ready for data exchange.

Parameters
serverthe calling server
connthe connection information

◆ RegisterConnectionReadyHandler()

void apollo::dreamview::WebSocketHandler::RegisterConnectionReadyHandler ( ConnectionReadyHandler  handler)
inline

Add a new handler for new connections.

Parameters
handlerThe function to handle the new connection in ReadyState.

◆ RegisterMessageHandler()

void apollo::dreamview::WebSocketHandler::RegisterMessageHandler ( std::string  type,
MessageHandler  handler 
)
inline

Add a new message handler for a message type.

Parameters
typeThe name/key to identify the message type.
handlerThe function to handle the received message.

◆ SendBinaryData()

bool apollo::dreamview::WebSocketHandler::SendBinaryData ( Connection conn,
const std::string &  data,
bool  skippable = false 
)

◆ SendData()

bool apollo::dreamview::WebSocketHandler::SendData ( Connection conn,
const std::string &  data,
bool  skippable = false,
int  op_code = MG_WEBSOCKET_OPCODE_TEXT 
)

Sends the provided data to a specific connected client.

Parameters
connThe connection to send to.
dataThe message string to be sent.
skippablewhether the data is allowed to be skipped if some other is being sent to this connection.

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