forked from Cylix/cpp_redis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathredis_subscriber.cpp
35 lines (27 loc) · 894 Bytes
/
redis_subscriber.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#include <cpp_redis/cpp_redis>
#include <signal.h>
#include <iostream>
volatile std::atomic_bool should_exit(false);
cpp_redis::redis_subscriber sub;
void
sigint_handler(int) {
std::cout << "disconnected (sigint handler)" << std::endl;
sub.disconnect();
}
int
main(void) {
sub.set_disconnection_handler([] (cpp_redis::redis_subscriber&) {
std::cout << "sub disconnected (disconnection handler)" << std::endl;
should_exit = true;
});
sub.connect();
sub.subscribe("some_chan", [] (const std::string& chan, const std::string& msg) {
std::cout << "MESSAGE " << chan << ": " << msg << std::endl;
});
sub.psubscribe("*", [] (const std::string& chan, const std::string& msg) {
std::cout << "PMESSAGE " << chan << ": " << msg << std::endl;
});
signal(SIGINT, &sigint_handler);
while (not should_exit);
return 0;
}