我是zeromq的新手,我稍微修改了zeromq示例以测试pub-sub模式的行为,订阅者订阅了两个主题“ABC”和“ABD”,一切顺利,但是当我重新启动发布者时,在订户方仅收到“ABD”。为什么呢?
子
#include "zhelpers.hpp"
int main ()
{
// Prepare our context and subscriber
zmq::context_t context(1);
zmq::socket_t subscriber (context, ZMQ_SUB);
subscriber.connect("tcp://localhost:5563");
subscriber.setsockopt( ZMQ_SUBSCRIBE, "ABC", 3);
subscriber.setsockopt( ZMQ_SUBSCRIBE, "ABD", 3);
while (1) {
// Read envelope with address
std::string address = s_recv (subscriber);
// Read message contents
std::string contents = s_recv (subscriber);
std::cout << "[" << address << "] " << contents << std::endl;
}
return 0;
}
酒馆
#include "zhelpers.hpp"
int main ()
{
// Prepare our context and publisher
zmq::context_t context(1);
zmq::socket_t publisher(context, ZMQ_PUB);
publisher.bind("tcp://*:5563");
while (1)
{
// Write two messages, each with an envelope and content
s_sendmore (publisher, "ABC");
s_send (publisher, "We don't want to see this");
s_sendmore (publisher, "ABD");
s_send (publisher, "We would like to see this");
sleep (1);
}
return 0;
}
输出
[ABC] We don't want to see this
[ABD] We would like to see this
[ABC] We don't want to see this
[ABD] We would like to see this
[ABC] We don't want to see this
[ABD] We would like to see this
//kill and restart publisher
[ABD] We would like to see this
[ABD] We would like to see this
[ABD] We would like to see this
[ABD] We would like to see this
[ABD] We would like to see this