1

我正在使用nanomsg的 C# 数据绑定。我有一个外部程序在 url ipc://report_data 上发送Google 协议缓冲区消息,并且我的订阅者连接到相同的确切 url。因此,我希望我的订阅者能够检索在该 url 上发送的任何数据,但事实并非如此。我使用函数 Receive() 并没有通过。该 URL 上只有一种类型的消息,因此我不关心该主题。有没有使用 nanomsg 经验的人知道如何读取传输 url 上的任何数据,无论主题如何?

这是我的订阅者和接收消息的代码:

public static void CreateSubscriber(string url, string topic)
{
    Console.WriteLine("\nCreating new subscriber with topic {0} and url {1}.", topic, url);

    var subscriber = new SubscribeSocket();

    subscriber.Connect(url);
    var sw = Stopwatch.StartNew();

    while (sw.Elapsed.TotalSeconds < 5000)
    {
        if (sw.Elapsed.TotalSeconds % 3 == 0)
        {
            Console.WriteLine("Checking for new data.");
            var streamOutput = ReceiveProtoBufferMessage(subscriber, topic);
        }
    }
    sw.Stop();
    Thread.Sleep(1);
    Console.WriteLine("Disposing subscriber.");
    subscriber.Dispose();
}

static byte[] ReceiveProtoBufferMessage(SubscribeSocket s, string topic)
{
    byte[] data = null;

    try
    {
        data = s.Receive();
        Console.WriteLine("Received data.");
    }
    catch
    {
        Console.WriteLine("Couldn't receive data.");
    }

    if (data != null)
    {
        Console.WriteLine("Data is not null.");
    }
    else
    {
        Console.WriteLine("Null data");
    }

    return data;
}
4

1 回答 1

0

想通了——为了让订阅者接收所有消息,我让订阅者订阅了一个空字符串主题:“”。

于 2017-02-20T21:34:05.800 回答