Routes incoming JSON messages to typed message-bus subscribers.
More...
#include <Malena/Providers/NetworkDispatcher.h>
|
| | NetworkDispatcher (const std::string &keyField="type") |
| void | dispatch (const json &msg) |
| | Dispatch a JSON object to the matching subscriber.
|
| void | offAllMessages () |
| | Unsubscribe from all messages registered by this object.
|
| template<typename DataType, typename Enum> |
| void | offMessage (Enum event) |
| | Unsubscribe from a specific typed message.
|
| void | on (const std::string &value, std::function< void(const json &)> callback) |
| | Subscribe a callback for a specific message value.
|
| template<typename DataType, typename Enum> |
| void | onMessage (Enum event, std::function< void(const DataType &)> callback) |
| | Register a callback to receive a typed message.
|
| template<typename DataType, typename Enum> |
| void | sendMessage (Enum event, const DataType &data) |
| | Publish a typed message to all current subscribers.
|
Routes incoming JSON messages to typed message-bus subscribers.
NetworkDispatcher reads a configurable key field (default "type") from each incoming JSON object and publishes its value on the global message bus. Subscribers call on() with the value they care about:
dispatcher.
on(
"student_connected", [](
const ml::json& msg) { ... });
dispatcher.
on(
"courses", [](
const ml::json& msg) { ... });
Routes incoming JSON messages to typed message-bus subscribers.
void on(const std::string &value, std::function< void(const json &)> callback)
Subscribe a callback for a specific message value.
NetworkDispatcher(const std::string &keyField="type")
nlohmann::json json
Alias for nlohmann::json.
- See also
- WebSocketProvider
Definition at line 37 of file NetworkDispatcher.h.
◆ NetworkDispatcher()
| ml::NetworkDispatcher::NetworkDispatcher |
( |
const std::string & | keyField = "type" | ) |
|
|
explicit |
◆ dispatch()
| void ml::NetworkDispatcher::dispatch |
( |
const json & | msg | ) |
|
Dispatch a JSON object to the matching subscriber.
Reads _keyField from the message and publishes its value on the message bus. No-op if the field is missing or empty.
- Parameters
-
| msg | Parsed JSON message from the network. |
◆ offAllMessages()
| void ml::Messenger::offAllMessages |
( |
| ) |
|
|
inherited |
Unsubscribe from all messages registered by this object.
Called automatically by the destructor. Only call manually if you need to reset all subscriptions while the object is still alive.
◆ offMessage()
template<typename DataType, typename Enum>
| void ml::Messenger::offMessage |
( |
Enum | event | ) |
|
|
inherited |
Unsubscribe from a specific typed message.
After this call, the callback previously registered for event and DataType will no longer be invoked. Safe to call from within the callback itself.
- Template Parameters
-
| DataType | The payload type of the subscription to remove. |
| Enum | The enum type identifying the event. |
- Parameters
-
| event | The specific event to unsubscribe from. |
◆ on()
| void ml::NetworkDispatcher::on |
( |
const std::string & | value, |
|
|
std::function< void(const json &)> | callback ) |
Subscribe a callback for a specific message value.
- Parameters
-
| value | The value of the key field to listen for. |
| callback | Called with the full JSON object when the value matches. |
◆ onMessage()
template<typename DataType, typename Enum>
| void ml::Messenger::onMessage |
( |
Enum | event, |
|
|
std::function< void(const DataType &)> | callback ) |
|
inherited |
Register a callback to receive a typed message.
The callback is invoked each time sendMessage is called with the same event and DataType. Multiple calls with the same event replace the previous callback for that event.
- Template Parameters
-
| DataType | The expected payload type. |
| Enum | The enum type identifying the event. |
- Parameters
-
| event | The specific event to listen for. |
| callback | Function called with a const reference to the payload when the event fires. |
◆ sendMessage()
template<typename DataType, typename Enum>
| void ml::Messenger::sendMessage |
( |
Enum | event, |
|
|
const DataType & | data ) |
|
inherited |
Publish a typed message to all current subscribers.
All objects that have called onMessage for the same event and DataType will have their callbacks invoked with data. Delivery is immediate unless MessageManager is currently iterating, in which case it is deferred.
- Template Parameters
-
| DataType | The payload type. Must match the type used in the corresponding onMessage call. |
| Enum | The enum type identifying the event. |
- Parameters
-
| event | The specific event to send. |
| data | The payload to deliver to subscribers. |
The documentation for this class was generated from the following file: