clients now can subscribe to be nofified when cnm receives a message

This commit is contained in:
Kjistóf 2016-12-05 12:54:38 +01:00
parent fa1078a313
commit e1f42bf31d

View File

@ -1,6 +1,7 @@
#pragma once
#include <iostream>
#include <string>
#include <vector>
#include <functional>
#include <boost/asio.hpp>
#include <algorithm>
@ -39,6 +40,7 @@ namespace chat
std::ostream _os;//--------'
std::string _login;
std::vector<std::function<void(chat_message)>> _subscriptions;
/* interface */
@ -74,6 +76,9 @@ namespace chat
});
}
void subscribe(std::function<void(chat_message)> 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<message>(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
});
}