callback function for advertise is not called
I have a publisher node and I would like to get a callback when a subscriber connects. I followed the example in the documentation, but I get a compilation warning and no callback. For example, I start a rostopic echo
before I launch my node, and I do receive the message, but the publisher is never notified that a subscriber connected.
Here is the warning:
In function ‘int main(int, char**)’:
.../AppTestCallbackSubscription.cpp:20:88: warning: the address of ‘void connectCallback(const ros::SingleSubscriberPublisher&)’ will always evaluate as ‘true’ [-Waddress]
Here is the code:
#include <ros/ros.h>
#include <std_msgs/String.h>
//typedef boost::function<void(const SingleSubscriberPublisher&)> SubscriberStatusCallback;
void connectCallback(const ros::SingleSubscriberPublisher& pub)
{
ROS_INFO("Subscriber connected");
}
int main(int argc, char** argv)
{
const std::string TOPIC_NAME = "/foo";
ros::init(argc, argv, "MyPublisher");
ros::NodeHandle nh("~");
// None of these two lines triggers the callback
//ros::Publisher pub = nh.advertise<std_msgs::String>(TOPIC_NAME, 10, connectCallback, ros::SubscriberStatusCallback(), ros::VoidConstPtr(), false);
ros::Publisher pub = nh.advertise<std_msgs::String>(TOPIC_NAME, 10, connectCallback);
// Gives time to the subscribers to connect
ros::Time maxTime(ros::Time::now()+ros::Duration(1)); // Will wait at most 1000 ms
while(ros::Time::now()<maxTime)
{
ros::spinOnce();
}
std_msgs::String msg;
msg.data = "Hello World!";
pub.publish(msg);
ROS_INFO("Message published");
return 0;
}