发布商在zeromq重启后订阅丢失

时间:2014-02-26 05:24:34

标签: zeromq publish-subscribe

我是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

1 个答案:

答案 0 :(得分:1)

我不知道你是否解决了这个问题,但我也面临着这种情况。这是ZMQ的一个问题,它实际上是在4.0.4版本中解决的(见this thread)。

问候。

相关问题