diff --git a/chat_networking.hpp b/chat_networking.hpp index 902204b..0114ccf 100644 --- a/chat_networking.hpp +++ b/chat_networking.hpp @@ -1,6 +1,7 @@ #pragma once #include #include +#include #include #include #include @@ -39,6 +40,7 @@ namespace chat std::ostream _os;//--------' std::string _login; + std::vector> _subscriptions; /* interface */ @@ -74,6 +76,9 @@ namespace chat }); } + void subscribe(std::function callback) + { _subscriptions.push_back(std::move(callback)); } + /* internal methods */ private: @@ -143,6 +148,7 @@ namespace chat char header = data[0]; // get command byte auto content = data.substr(1, data.size()-1); // minus command & term bytes + chat_message msg(static_cast(header), content); switch (header) // decide what to do { @@ -167,6 +173,9 @@ namespace chat break; } + for (auto& fun : _subscriptions) // notify subscribers + fun(msg); + receive(); // receive the next message }); }