【问题标题】:subscription lost after publisher restart in zeromq在 zeromq 中发布者重新启动后订阅丢失
【发布时间】:2014-02-26 05:24:33
【问题描述】:

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

【问题讨论】:

  • 您使用的是哪个版本的 0MQ?我无法重现 3.2.3 的问题。

标签: zeromq publish-subscribe


【解决方案1】:

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

问候。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多