27 #include <unordered_map> 30 #include "CivetServer.h" 31 #include "nlohmann/json.hpp" 49 thread_local
static unsigned char current_opcode_;
50 thread_local
static std::stringstream data_;
53 using Json = nlohmann::json;
102 size_t data_len)
override;
119 bool BroadcastData(
const std::string &data,
bool skippable =
false);
130 bool skippable =
false,
int op_code = MG_WEBSOCKET_OPCODE_TEXT);
133 bool skippable =
false);
141 message_handlers_[type] = handler;
149 connection_ready_handlers_.emplace_back(handler);
153 const std::string name_;
156 std::unordered_map<std::string, MessageHandler> message_handlers_;
158 std::vector<ConnectionReadyHandler> connection_ready_handlers_;
165 mutable std::mutex mutex_;
169 std::unordered_map<Connection *, std::shared_ptr<std::mutex>> connections_;
nlohmann::json Json
Definition: websocket_handler.h:53
bool SendBinaryData(Connection *conn, const std::string &data, bool skippable=false)
WebSocketHandler(const std::string &name)
Definition: websocket_handler.h:58
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
void RegisterMessageHandler(std::string type, MessageHandler handler)
Add a new message handler for a message type.
Definition: websocket_handler.h:140
void RegisterConnectionReadyHandler(ConnectionReadyHandler handler)
Add a new handler for new connections.
Definition: websocket_handler.h:148
bool BroadcastData(const std::string &data, bool skippable=false)
Sends the provided data to all the connected clients.
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.
struct mg_connection Connection
Definition: websocket_handler.h:54
bool handleBinaryData(Connection *conn, const std::string &data)
void handleReadyState(CivetServer *server, Connection *conn) override
Callback method for when websocket handshake is successfully completed, and connection is ready for d...
void handleClose(CivetServer *server, const Connection *conn) override
Callback method for when the connection is closed.
bool handleJsonData(Connection *conn, const std::string &data)
std::function< void(Connection *)> ConnectionReadyHandler
Definition: websocket_handler.h:56
bool handleConnection(CivetServer *server, const Connection *conn) override
Callback method for when the client intends to establish a websocket connection, before websocket han...
Definition: websocket_handler.h:68
The WebSocketHandler, built on top of CivetWebSocketHandler, is a websocket handler that handles diff...
Definition: websocket_handler.h:46
std::function< void(const Json &, Connection *)> MessageHandler
Definition: websocket_handler.h:55
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.