Add optional callback for messages on channels without handler.
This commit is contained in:
parent
b5ac54b0aa
commit
f7683abf0b
|
@ -272,11 +272,19 @@ namespace resply {
|
|||
Client& psubscribe(const std::string& pattern, ChannelCallback callback);
|
||||
|
||||
/*! \brief Puts the client into subscribed mode.
|
||||
* \param other Message callback for channels subscribed to using #command.
|
||||
*
|
||||
* This method will not return until the client has been unsubscribed
|
||||
* from all channels.
|
||||
*/
|
||||
void listen_for_messages();
|
||||
void listen_for_messages(ChannelCallback other);
|
||||
|
||||
/*! \brief Puts the client into subscribed mode.
|
||||
*
|
||||
* Convience method which just ignores auxiliary messages.
|
||||
*/
|
||||
void listen_for_messages() { listen_for_messages([](auto, auto) {}); }
|
||||
|
||||
|
||||
private:
|
||||
/*! \brief Sends the command to the server.
|
||||
|
|
|
@ -139,7 +139,7 @@ public:
|
|||
return receive_responses(commands.size());
|
||||
}
|
||||
|
||||
void listen_for_messages()
|
||||
void listen_for_messages(ChannelCallback other)
|
||||
{
|
||||
for (;;) {
|
||||
Result result{receive_response()};
|
||||
|
@ -149,7 +149,11 @@ public:
|
|||
std::string& channel = result.array[1].string;
|
||||
std::string& message = result.array[2].string;
|
||||
|
||||
channel_callbacks_[channel](channel, message);
|
||||
if (channel_callbacks_.count(channel)) {
|
||||
channel_callbacks_[channel](channel, message);
|
||||
} else {
|
||||
other(channel, message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -272,9 +276,9 @@ Result Client::finish_command(const std::string& command)
|
|||
return command.empty() ? Result{} : impl_->send(command);
|
||||
}
|
||||
|
||||
void Client::listen_for_messages()
|
||||
void Client::listen_for_messages(ChannelCallback other)
|
||||
{
|
||||
impl_->listen_for_messages();
|
||||
impl_->listen_for_messages(other);
|
||||
}
|
||||
|
||||
std::vector<Result> Client::Pipeline::send()
|
||||
|
|
Reference in a new issue