C# StackExchange.Redis如何订阅多个频道
如何订阅多个频道?据我所知,需要传递给Subscribe方法的Channel类支持模式或单通道订阅。是否可以在一个命令中订阅多个频道 例如: 客户端正在3个不同的频道上发布:“ChannelA”、“ChannelB”和“ChannelC”。如何在一个命令中订阅这些频道?我知道我可以使用像“Channel*”这样的模式,但是,如果这些通道不能用单个模式来描述呢?订阅“ChannelA、ChannelB、ChannelC”似乎不起作用-我无法从ChannelMessageQueue获取任何消息。您可以使用:C# StackExchange.Redis如何订阅多个频道,c#,redis,C#,Redis,如何订阅多个频道?据我所知,需要传递给Subscribe方法的Channel类支持模式或单通道订阅。是否可以在一个命令中订阅多个频道 例如: 客户端正在3个不同的频道上发布:“ChannelA”、“ChannelB”和“ChannelC”。如何在一个命令中订阅这些频道?我知道我可以使用像“Channel*”这样的模式,但是,如果这些通道不能用单个模式来描述呢?订阅“ChannelA、ChannelB、ChannelC”似乎不起作用-我无法从ChannelMessageQueue获取任何消息。您可
PSU订阅频道*
应该收听以“channel”开头的任何频道
或者,您可以与多个频道一起使用:
SUBSCRIBE ChannelA ChannelB ChannelMessageQueue
在IRedisSubscription接口下使用了psubscribe,然后我们可以使用
subscription.subscriptbetochannelsmatching在c#中,只需根据您的需要提供模式,如这一行
subscription.SubscribeToChannelsMatching(_config.ActiveChannelName)强>
我将消息合并到mongodb的示例用法:
public void SubScribeChannel()
{
string channelName = _config.ActiveChannelName;
using (var redisConsumer = new RedisClient(_config.SingleHost))
using (var subscription = redisConsumer.CreateSubscription())
{
subscription.OnSubscribe = channel =>
{
Debug.WriteLine(String.Format("Subscribed to '{0}'", channel));
};
subscription.OnUnSubscribe = channel =>
{
Debug.WriteLine(String.Format("UnSubscribed from '{0}'", channel));
};
subscription.OnMessage = async (channel, msg) =>
{
Debug.WriteLine(String.Format("Received '{0}' from channel '{1}'", msg, channel));
List<Document> documents = Transformer.Deserialize<List<Document>>(msg);
await MergeToMongoDb(documents, channelName);
};
try
{
Debug.WriteLine(String.Format("SubscribeToChannels: '{0}'", channelName));
//subscription.SubscribeToChannels(channelName);
subscription.SubscribeToChannelsMatching(_config.ActiveChannelName);
}
catch(Exception ex)
{
throw ex;
}
}
Debug.WriteLine("EOF");
}
public void SubScribeChannel()
{
字符串channelName=\u config.ActiveChannelName;
使用(var redisConsumer=new RedisClient(_config.SingleHost))
使用(var subscription=redisConsumer.CreateSubscription())
{
subscription.onsubscripte=频道=>
{
WriteLine(String.Format(“订阅了{0}',通道));
};
subscription.onunsubscripte=频道=>
{
WriteLine(String.Format(“从{0}'取消订阅,频道));
};
subscription.OnMessage=async(频道,消息)=>
{
Debug.WriteLine(String.Format(“从通道{1}'、消息、通道接收到{0}”);
列表文档=Transformer.Deserialize(msg);
等待合并数据库(文件、渠道名称);
};
尝试
{
Debug.WriteLine(String.Format(“SubscribeToChannels:'{0}',channelName));
//订阅。订阅通道(通道名称);
subscription.SubscribeToChannelsMatching(_config.ActiveChannelName);
}
捕获(例外情况除外)
{
掷骰子;
}
}
Debug.WriteLine(“EOF”);
}
ActiveChannelName的值是类似“TestChannel*”的模式