Orbit Framework 2.0.0
A high-performance asynchronous C++20 HTTP server framework.
Loading...
Searching...
No Matches
orbit::websocket::EventRouter< SessionType > Class Template Reference

A Socket.IO style event router for WebSockets with Room and Session support. More...

#include <EventRouter.hpp>

Classes

struct  RoomBroadcaster
 

Public Types

using SocketPtr = std::shared_ptr< EventSocket< SessionType > >
 

Public Member Functions

template<typename PayloadType >
void on (const std::string &event, std::function< void(EventSocket< SessionType > &, const PayloadType &)> handler)
 
void on (const std::string &event, std::function< void(EventSocket< SessionType > &)> handler)
 Registers an untyped event handler (no data expected).
 
void on_connect (std::function< void(EventSocket< SessionType > &)> handler)
 Registers a connection handler.
 
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 authenticated data (e.g. req.user set by jwt_auth() in attach()'s middleware) rather than from what the client sends later. The request is valid only during the call.
 
void on_disconnect (std::function< void(EventSocket< SessionType > &)> handler)
 Registers a disconnection handler.
 
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.
 
RoomBroadcaster to (const std::string &room)
 
void join_room (const std::string &id, const std::string &room)
 
void leave_room (const std::string &id, const std::string &room)
 
void broadcast_to_room (const std::string &room, const std::string &msg)
 

Private Member Functions

SocketPtr get_socket (const std::string &id)
 

Private Attributes

std::mutex mutex_
 
std::unordered_map< std::string, SocketPtr > sockets_
 
std::unordered_map< std::string, std::unordered_set< std::string > > rooms_
 
std::unordered_map< std::string, std::function< void(EventSocket< SessionType > &, const nlohmann::json &)> > handlers_
 
std::function< void(EventSocket< SessionType > &, const http::HttpRequest &)> on_connect_
 
std::function< void(EventSocket< SessionType > &)> on_disconnect_
 

Detailed Description

template<typename SessionType>
class orbit::websocket::EventRouter< SessionType >

A Socket.IO style event router for WebSockets with Room and Session support.

Member Typedef Documentation

◆ SocketPtr

Member Function Documentation

◆ attach()

template<typename SessionType >
void orbit::websocket::EventRouter< SessionType >::attach ( server::App &  app,
const std::string &  path,
std::vector< routing::Middleware >  mws = {} 
)
inline

Attaches this EventRouter to the Orbit App at a specific path.

Parameters
mwsMiddleware run on the handshake before the upgrade, as for app.ws(): require_origin() (browsers do not apply CORS to WebSockets), jwt_auth(), rate limits. One that stops the request refuses the connection.
Examples
/home/runner/work/orbit-framework/orbit-framework/include/orbit/websocket/EventRouter.hpp.

◆ broadcast_to_room()

◆ get_socket()

◆ join_room()

◆ leave_room()

◆ on() [1/2]

template<typename SessionType >
void orbit::websocket::EventRouter< SessionType >::on ( const std::string &  event,
std::function< void(EventSocket< SessionType > &)>  handler 
)
inline

Registers an untyped event handler (no data expected).

◆ on() [2/2]

◆ on_connect() [1/2]

template<typename SessionType >
void orbit::websocket::EventRouter< SessionType >::on_connect ( std::function< void(EventSocket< SessionType > &)>  handler)
inline

◆ on_connect() [2/2]

template<typename SessionType >
void orbit::websocket::EventRouter< SessionType >::on_connect ( std::function< void(EventSocket< SessionType > &, const http::HttpRequest &)>  handler)
inline

Registers a connection handler that also sees the handshake request, to initialise the session from authenticated data (e.g. req.user set by jwt_auth() in attach()'s middleware) rather than from what the client sends later. The request is valid only during the call.

◆ on_disconnect()

template<typename SessionType >
void orbit::websocket::EventRouter< SessionType >::on_disconnect ( std::function< void(EventSocket< SessionType > &)>  handler)
inline

◆ to()

Member Data Documentation

◆ handlers_

◆ mutex_

◆ on_connect_

◆ on_disconnect_

◆ rooms_

template<typename SessionType >
std::unordered_map<std::string, std::unordered_set<std::string> > orbit::websocket::EventRouter< SessionType >::rooms_
private

◆ sockets_


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