2017-02-17 187 views
1

我正在使用C#数据绑定为nanomsg。我有一个外部程序在url ipc:// report_data上发送Google Protocol Buffer消息,我的订阅者连接到同一个确切的url。所以,我希望我的订阅者能够检索在该URL上发送的任何数据,但事实并非如此。我使用函数Receive()并且没有任何东西通过。只有一种类型的消息通过该URL传递,所以我不关心这个话题。有没有人有nanomsg的经验知道如何读取运输网址上的任何数据,而不管主题?使用nanomsg从发布商订阅/接收数据。

这是我的用户和接收消息的代码:

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; 
} 
+0

你为什么要做'sw.Elapsed.TotalSeconds%3 == 0'? 'sw.Elapsed.TotalSeconds'是一个'double',所以不能保证做这样的模数将会是'0'。是否显示消息“检查新数据”? – Enigmativity

回答

0

想通了 - 为了让用户拿起所有的消息我所做的用户订阅一个空字符串主题:“”。