@@ -38,21 +38,20 @@ To build the tests, it is necessary to install `google_tests`. Just run the `ins
3838
3939### Methods
4040
41- #### void connect(const std::string& host = "127.0.0.1", unsigned int port = 6379)
41+ #### void connect(const std::string& host = "127.0.0.1", unsigned int port = 6379, const disconnection_handler& handler = nullptr )
4242Connect to the Redis Server. Connection is done synchronously.
4343Throws redis_error in case of failure or if client if already connected.
4444
45+ Also set the disconnection handler which is called whenever a disconnection has occurred.
46+ Disconnection handler is an ` std::function<void(redis_client&)> ` .
47+
4548#### void disconnect(void)
4649Disconnect client from remote host.
4750Throws redis_error if client is not connected to any server.
4851
4952#### bool is_connected(void)
5053Returns whether the client is connected or not.
5154
52- #### void set_disconnection_handler(const disconnection_handler& handler)
53- Set the disconnection handler which is called whenever a disconnection has occurred.
54- Disconnection handler is an ` std::function<void(redis_client&)> ` .
55-
5655#### void send(const std::vector< std::string > & redis_cmd, const reply_callback& callback = nullptr)
5756Send a command and set the callback which has to be called when the reply has been received.
5857If ` nullptr ` is passed as callback, command is executed and no callback will be called.
@@ -78,13 +77,11 @@ sigint_handler(int) {
7877
7978int
8079main(void) {
81- client.set_disconnection_handler( [ ] (cpp_redis::redis_client&) {
80+ client.connect("127.0.0.1", 6379, [ ] (cpp_redis::redis_client&) {
8281 std::cout << "client disconnected (disconnection handler)" << std::endl;
8382 should_exit = true;
8483 });
8584
86- client.connect();
87-
8885 client.send({"SET", "hello", "world"}, [ ] (cpp_redis::reply& reply) {
8986 std::cout << reply.as_string() << std::endl;
9087 });
@@ -103,21 +100,20 @@ main(void) {
103100
104101### Methods
105102
106- #### void connect(const std::string& host = "127.0.0.1", unsigned int port = 6379)
103+ #### void connect(const std::string& host = "127.0.0.1", unsigned int port = 6379, const disconnection_handler& handler = nullptr )
107104Connect to the Redis Server. Connection is done synchronously.
108105Throws redis_error in case of failure or if client if already connected.
109106
107+ Also set the disconnection handler which is called whenever a disconnection has occurred.
108+ Disconnection handler is an `std::function<void(redis_subscriber&)>`.
109+
110110#### void disconnect(void)
111111Disconnect client from remote host.
112112Throws redis_error if client is not connected to any server.
113113
114114#### bool is_connected(void)
115115Returns whether the client is connected or not.
116116
117- #### void set_disconnection_handler(const disconnection_handler& handler)
118- Set the disconnection handler which is called whenever a disconnection has occurred.
119- Disconnection handler is an `std::function<void(redis_subscriber&)>`.
120-
121117#### void subscribe(const std::string& channel, const subscribe_callback& callback)
122118Subscribe to the given channel and call subscribe_callback each time a message is published in this channel.
123119subscribe_callback is an `std::function<void(const std::string&, const std::string&)>`.
@@ -145,31 +141,29 @@ cpp_redis::redis_subscriber sub;
145141
146142void
147143sigint_handler(int) {
148- std::cout << "disconnected (sigint handler)" << std::endl;
149- sub.disconnect();
150- should_exit = true;
144+ std::cout << "disconnected (sigint handler)" << std::endl;
145+ sub.disconnect();
146+ should_exit = true;
151147}
152148
153149int
154150main(void) {
155- sub.set_disconnection_handler([] (cpp_redis::redis_subscriber&) {
156- std::cout << "sub disconnected (disconnection handler)" << std::endl;
157- should_exit = true;
158- });
159-
160- sub.connect();
151+ sub.connect("127.0.0.1", 6379, [](cpp_redis::redis_subscriber&) {
152+ std::cout << "sub disconnected (disconnection handler)" << std::endl;
153+ should_exit = true;
154+ });
161155
162- sub.subscribe("some_chan", [] (const std::string& chan, const std::string& msg) {
163- std::cout << "MESSAGE " << chan << ": " << msg << std::endl;
164- });
165- sub.psubscribe("*", [] (const std::string& chan, const std::string& msg) {
166- std::cout << "PMESSAGE " << chan << ": " << msg << std::endl;
167- });
156+ sub.subscribe("some_chan", [] (const std::string& chan, const std::string& msg) {
157+ std::cout << "MESSAGE " << chan << ": " << msg << std::endl;
158+ });
159+ sub.psubscribe("*", [] (const std::string& chan, const std::string& msg) {
160+ std::cout << "PMESSAGE " << chan << ": " << msg << std::endl;
161+ });
168162
169- signal(SIGINT, &sigint_handler);
170- while (not should_exit);
163+ signal(SIGINT, &sigint_handler);
164+ while (not should_exit);
171165
172- return 0;
166+ return 0;
173167}
174168```
175169
0 commit comments