2016-04-30 37 views
1

我正在使用Sider C#Redis客戶端連接到運行在我的Windows 7計算機上的Redis服務器。從我的C#應用​​程序 https://github.com/chakrit/sider使用發佈/訂閱sider redis C#客戶端

我能放火/獲取/選擇

我現在想使用發佈/訂閱功能,使我的C#應用​​程序,可在Redis的任何更改通知客戶的「鑰匙」以的方式(通過代表)

我無法編寫代碼,因爲沒有關於如何使用sider客戶端頁面的示例。

所有我可以寫是這樣的:

var client = new RedisClient(address, 6379); 
string[] keys = new string[1]; 
keys[0] = "key1ToMonitor"; 
IObservable<Message<string>> obb = client.Subscribe(keys); 

我知道這看起來跛,但我有不知道如何,如果任何客戶端更改所需的鍵將其寫在拉姆達的方式在我的功能將被稱爲redis服務器。 PS:我是新手,如果我的方法有缺陷,那麼糾正我。

編輯:關於添加建議的更改,我收到以下錯誤。

Error 7 Cannot convert lambda expression to type 'System.IObserver<Sider.Message<string>>' because it is not a delegate type D:\_Work\TestApp\Program.cs 90 27 TestApp 

的obb.subscribe簽名看起來像這樣

namespace System 
{ 
    // Summary: 
    //  Defines a provider for push-based notification. 
    // 
    // Type parameters: 
    // T: 
    //  The object that provides notification information.This type parameter is 
    //  covariant. That is, you can use either the type you specified or any type 
    //  that is more derived. For more information about covariance and contravariance, 
    //  see Covariance and Contravariance in Generics. 
    public interface IObservable<out T> 
    { 
     // Summary: 
     //  Notifies the provider that an observer is to receive notifications. 
     // 
     // Parameters: 
     // observer: 
     //  The object that is to receive notifications. 
     // 
     // Returns: 
     //  The observer's interface that enables resources to be disposed. 
     IDisposable Subscribe(IObserver<T> observer); 
    } 
} 

代碼:

 var client = new RedisClient(address, 6379); 
     string[] keys = new string[1]; 
     keys[0] = "key1ToMonitor"; 
     IObservable<Message<string>> obb = client.Subscribe(keys); 
     obb.Subscribe(x => Debug.WriteLine(x.ToString())); // error : doesn't let me compile 
+0

該庫似乎有一個奇怪的「可觀察」語法,因此您必須再次訂閱obb才能看到結果。試試'obb.Subscribe(x => Debug.WriteLine(x.ToString()))'讓我知道你會得到什麼結果。 – Enigmativity

+0

您是否可以在代碼中準確顯示您如何嘗試建議的更改? – Enigmativity

+0

@Enigmativity code added – ankur

回答

0

您需要訂閱產生的實際觀測。事情是這樣的:

obb.Subscribe(x => Debug.WriteLine(x.ToString())); 

不要忘記添加using System.Reactive.Linq;去一個lambda轉換成觀察者所需要的擴展。