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);
136 bool skippable =
false,
int op_code = MG_WEBSOCKET_OPCODE_TEXT);
139 bool skippable =
false);
147 message_handlers_[type] = handler;
155 connection_ready_handlers_.emplace_back(handler);
159 const std::string name_;
162 std::unordered_map<std::string, MessageHandler> message_handlers_;
164 std::vector<ConnectionReadyHandler> connection_ready_handlers_;
171 mutable std::mutex mutex_;
175 std::unordered_map<Connection *, std::shared_ptr<std::mutex>> connections_;
The WebSocketHandler, built on top of CivetWebSocketHandler, is a websocket handler that handles diff...
WebSocketHandler(const std::string &name)
bool BroadcastData(const std::string &data, bool skippable=false)
Sends the provided data to all the connected clients.
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.
bool BroadcastBinaryData(const std::string &data, bool skippable=false)
Sends the provided binary data to all the connected clients.
void RegisterMessageHandler(std::string type, MessageHandler handler)
Add a new message handler for a message type.
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.
std::function< void(const Json &, Connection *)> MessageHandler
std::function< void(Connection *)> ConnectionReadyHandler
bool handleBinaryData(Connection *conn, const std::string &data)
bool handleConnection(CivetServer *server, const Connection *conn) override
Callback method for when the client intends to establish a websocket connection, before websocket han...
void handleClose(CivetServer *server, const Connection *conn) override
Callback method for when the connection is closed.
bool handleJsonData(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 RegisterConnectionReadyHandler(ConnectionReadyHandler handler)
Add a new handler for new connections.
bool SendBinaryData(Connection *conn, const std::string &data, bool skippable=false)