|
| | WebSocketProvider (const std::string &keyField="type") |
| void | connect (const std::string &url) |
| | Open a WebSocket connection to url.
|
| void | disconnect () |
| | Close the connection gracefully.
|
| 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.
|
| void | onClose (std::function< void(int, const std::string &)> callback) |
| | Called on the main thread when the connection closes.
|
| void | onError (std::function< void(const std::string &)> callback) |
| | Called on the main thread on a protocol or network error.
|
| template<typename DataType, typename Enum> |
| void | onMessage (Enum event, std::function< void(const DataType &)> callback) |
| | Register a callback to receive a typed message.
|
| void | onOpen (std::function< void()> callback) |
| | Called on the main thread when the connection opens.
|
| void | send (const json &msg) |
| | Send a JSON object as a UTF-8 text frame.
|
| void | send (const std::string &text) |
| | Send a raw text frame.
|
| template<typename DataType, typename Enum> |
| void | sendMessage (Enum event, const DataType &data) |
| | Publish a typed message to all current subscribers.
|
| void | setMaxReconnectAttempts (int n) |
| | Maximum reconnect attempts. Pass -1 for unlimited.
|
| void | setReconnectDelay (int seconds) |
| | Delay in seconds before reconnecting after an unexpected close.
|
WebSocket connection with automatic JSON dispatch.
WebSocketProvider combines a WebSocketClient (raw transport) with a NetworkDispatcher (typed routing) into a single reusable object.
Create one in your application class, call on() in each hook's init() to declare which message types it cares about, then call connect() once to open the connection:
_network.onOpen([this]{
_network.send({{"type","register"}, {"role","admin"}});
});
_network.connect("ws://localhost:3000");
_chatHook->init(_network);
_studentsHook->init(_network);
{
network.
on(
"student_message", [
this](
const ml::json& msg) {
handleStudentMessage(msg["from"], msg["text"], msg.value("ts", ""));
});
sendHook(AppShell::Hook::GRAPHIC);
}
void on(const std::string &value, std::function< void(const json &)> callback)
Subscribe a callback for a specific message value.
WebSocket connection with automatic JSON dispatch.
nlohmann::json json
Alias for nlohmann::json.
- See also
- NetworkDispatcher, WebSocketClient
Definition at line 51 of file WebSocketProvider.h.
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. |
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. |