2017-02-17 188 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

想通了 - 爲了讓用戶拿起所有的消息我所做的用戶訂閱一個空字符串主題:「」。