8#include <unordered_map>
9#include <unordered_set>
19template <
typename SessionType = EmptySession>
25template <
typename SessionType>
43 void join(
const std::string& room);
48 void leave(
const std::string& room);
53 auto to(
const std::string& room);
60 const std::string&
id()
const {
return id_; }
70 std::unordered_set<std::string>
rooms_;
78template <
typename SessionType>
81 using SocketPtr = std::shared_ptr<EventSocket<SessionType>>;
89 template <
typename PayloadType>
95 }
catch (
const std::exception& e) {
97 LOG_DEBUG(
"[EventRouter] Invalid payload for event '" <<
event <<
"': " << e.what());
149 static std::atomic<uint64_t>
counter = 0;
150 std::string
id =
"ws_" + std::to_string(++
counter) +
"_" + std::to_string(
reinterpret_cast<uint64_t>(&
raw_ws));
152 auto es = std::make_shared<EventSocket<SessionType>>(
raw_ws, *
this, id);
162 on_connect_(*es, handshake ? *handshake : kNoRequest);
165 raw_ws.on_message([
this,
id](
const std::string& msg) {
168 if (!j.contains(
"event"))
return;
170 std::string event_name = j[
"event"];
177 it->second(*socket, data);
185 raw_ws.on_close([
this,
id]() {
189 std::lock_guard<std::mutex> lock(
mutex_);
191 for (
const auto& room : socket->
rooms_) {
205 template <
typename T>
218 void join_room(
const std::string&
id,
const std::string& room) {
226 void leave_room(
const std::string&
id,
const std::string& room) {
237 for (
const std::string&
id :
rooms_[room]) {
254 std::unordered_map<std::string, SocketPtr>
sockets_;
255 std::unordered_map<std::string, std::unordered_set<std::string>>
rooms_;
262template <
typename SessionType>
267template <
typename SessionType>
272template <
typename SessionType>
#define LOG_DEBUG(msg)
Definition Logger.hpp:69
a class to store JSON values
Definition json.hpp:19401
static basic_json parse(InputType &&i, const parser_callback_t cb=nullptr, const bool allow_exceptions=true, const bool ignore_comments=false)
deserialize from a compatible input
Definition json.hpp:23317
bool contains(const typename object_t::key_type &key) const
check the existence of an element in a JSON object
Definition json.hpp:22031
Represents an active WebSocket connection.
Definition WebSocketConnection.hpp:88
void send(const std::string &message)
Sends a text message over the WebSocket connection. Safe to call from any thread.
The main application class for the Orbit Framework.
Definition App.hpp:36
A Socket.IO style event router for WebSockets with Room and Session support.
Definition EventRouter.hpp:79
std::unordered_map< std::string, SocketPtr > sockets_
Definition EventRouter.hpp:254
void join_room(const std::string &id, const std::string &room)
Definition EventRouter.hpp:218
void broadcast_to_room(const std::string &room, const std::string &msg)
Definition EventRouter.hpp:234
void on_disconnect(std::function< void(EventSocket< SessionType > &)> handler)
Registers a disconnection handler.
Definition EventRouter.hpp:134
std::shared_ptr< EventSocket< SessionType > > SocketPtr
Definition EventRouter.hpp:81
void attach(server::App &app, const std::string &path, std::vector< routing::Middleware > mws={})
Attaches this EventRouter to the Orbit App at a specific path.
Definition EventRouter.hpp:145
std::unordered_map< std::string, std::function< void(EventSocket< SessionType > &, const nlohmann::json &)> > handlers_
Definition EventRouter.hpp:256
void on(const std::string &event, std::function< void(EventSocket< SessionType > &, const PayloadType &)> handler)
Definition EventRouter.hpp:90
void on(const std::string &event, std::function< void(EventSocket< SessionType > &)> handler)
Registers an untyped event handler (no data expected).
Definition EventRouter.hpp:105
SocketPtr get_socket(const std::string &id)
Definition EventRouter.hpp:246
std::function< void(EventSocket< SessionType > &, const http::HttpRequest &)> on_connect_
Definition EventRouter.hpp:258
std::function< void(EventSocket< SessionType > &)> on_disconnect_
Definition EventRouter.hpp:259
void leave_room(const std::string &id, const std::string &room)
Definition EventRouter.hpp:226
std::unordered_map< std::string, std::unordered_set< std::string > > rooms_
Definition EventRouter.hpp:255
std::mutex mutex_
Definition EventRouter.hpp:253
void on_connect(std::function< void(EventSocket< SessionType > &, const http::HttpRequest &)> handler)
Registers a connection handler that also sees the handshake request, to initialise the session from a...
Definition EventRouter.hpp:127
void on_connect(std::function< void(EventSocket< SessionType > &)> handler)
Registers a connection handler.
Definition EventRouter.hpp:114
RoomBroadcaster to(const std::string &room)
Definition EventRouter.hpp:213
Represents a connected client in the EventRouter.
Definition EventRouter.hpp:26
http::websocket::WebSocketConnection & raw()
Definition EventRouter.hpp:63
std::string id_
Definition EventRouter.hpp:68
void leave(const std::string &room)
Leaves a specific room/channel.
Definition EventRouter.hpp:268
http::websocket::WebSocketConnection & raw_
Definition EventRouter.hpp:66
EventSocket(http::websocket::WebSocketConnection &raw, EventRouter< SessionType > &router, std::string id)
Definition EventRouter.hpp:28
EventRouter< SessionType > & router_
Definition EventRouter.hpp:67
const std::string & id() const
Definition EventRouter.hpp:60
SessionType session_
Definition EventRouter.hpp:69
void join(const std::string &room)
Joins a specific room/channel.
Definition EventRouter.hpp:263
SessionType & session()
The strongly-typed session state attached to this connection.
Definition EventRouter.hpp:58
auto to(const std::string &room)
Returns a broadcaster to emit messages to everyone in the room.
Definition EventRouter.hpp:273
void emit(const std::string &event, const T &data)
Emits an event with strongly-typed data back to this specific client.
Definition EventRouter.hpp:35
std::unordered_set< std::string > rooms_
Definition EventRouter.hpp:70
basic_json<> json
default specialization
Definition json.hpp:3422
Definition legacy_namespaces.hpp:27
Represents an incoming HTTP request.
Definition HttpRequest.hpp:54
Definition EventRouter.hpp:17
Definition EventRouter.hpp:201
void emit(const std::string &event, const T &data)
Definition EventRouter.hpp:206
EventRouter & router
Definition EventRouter.hpp:202
std::string room
Definition EventRouter.hpp:203