不,ZMQ中没有回调系统。你必须打电话
recv()
函数以接收消息。
您可以使用
recv()
if
条件和a
while
环
我经常使用这样一种带有超时的模式:
zmq::context_t zmq_context(1);
zmq::socket_t zmq_socket(zmq_context, ZMQ_SUB);
zmq_socket.connect("tcp://127.0.0.1:58951");
std::string TOPIC = "";
zmq_socket.setsockopt(ZMQ_SUBSCRIBE, TOPIC.c_str(), TOPIC.length()); // allow all messages
zmq_socket.setsockopt(ZMQ_RCVTIMEO, 1000); // Timeout to get out of the while loop since recv is blocking
while(run) {
zmq::message_t msg;
int rc = zmq_socket.recv(&msg); // Is blocking until you receive a message
if(rc) {
// You received a message : your data is in msg
// Do stuff here : call your function with the parameters received from zmq
}
}
// Clean up your socket and context here
zmq_socket.setsockopt(ZMQ_LINGER, linger);
zmq_socket.close();
zmq_context.close();