使用发布/订阅sider redis C#客户端
问题描述:
我正在使用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
答
您需要订阅产生的实际观测。事情是这样的:
obb.Subscribe(x => Debug.WriteLine(x.ToString()));
不要忘记添加using System.Reactive.Linq;
去一个lambda转换成观察者所需要的扩展。
该库似乎有一个奇怪的“可观察”语法,因此您必须再次订阅obb才能看到结果。试试'obb.Subscribe(x => Debug.WriteLine(x.ToString()))'让我知道你会得到什么结果。 – Enigmativity
您是否可以在代码中准确显示您如何尝试建议的更改? – Enigmativity
@Enigmativity code added – ankur