clients now can subscribe to be nofified when cnm receives a message
This commit is contained in:
parent
fa1078a313
commit
e1f42bf31d
@ -1,6 +1,7 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <boost/asio.hpp>
|
#include <boost/asio.hpp>
|
||||||
#include <algorithm>
|
#include <algorithm>
|
||||||
@ -39,6 +40,7 @@ namespace chat
|
|||||||
std::ostream _os;//--------'
|
std::ostream _os;//--------'
|
||||||
|
|
||||||
std::string _login;
|
std::string _login;
|
||||||
|
std::vector<std::function<void(chat_message)>> _subscriptions;
|
||||||
|
|
||||||
|
|
||||||
/* interface */
|
/* interface */
|
||||||
@ -74,6 +76,9 @@ namespace chat
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void subscribe(std::function<void(chat_message)> callback)
|
||||||
|
{ _subscriptions.push_back(std::move(callback)); }
|
||||||
|
|
||||||
|
|
||||||
/* internal methods */
|
/* internal methods */
|
||||||
private:
|
private:
|
||||||
@ -143,6 +148,7 @@ namespace chat
|
|||||||
|
|
||||||
char header = data[0]; // get command byte
|
char header = data[0]; // get command byte
|
||||||
auto content = data.substr(1, data.size()-1); // minus command & term bytes
|
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
|
switch (header) // decide what to do
|
||||||
{
|
{
|
||||||
@ -167,6 +173,9 @@ namespace chat
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
for (auto& fun : _subscriptions) // notify subscribers
|
||||||
|
fun(msg);
|
||||||
|
|
||||||
receive(); // receive the next message
|
receive(); // receive the next message
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user