我如何订阅多个频道?据我了解,需要传递给Subscribe方法的Channel类支持模式或单通道订阅。一个命令可以订阅多个频道吗?
示例: 客户端正在3个不同的渠道上发布:“ ChannelA”,“ ChannelB”和“ ChannelC”。如何在一个命令中订阅这些频道?我知道我可以使用“通道*”之类的模式,但是,如果那些通道不能用单个模式来描述怎么办?订阅“ ChannelA,ChannelB,ChannelC”似乎不起作用-我无法从ChannelMessageQueue获得任何消息。
答案 0 :(得分:0)
您可以使用PSUBSCRIBE:
PSUBSCRIBE Channel*
那应该收听以“ Channel”开头的任何频道。
或者您可以将SUBSCRIBE用于多个渠道:
SUBSCRIBE ChannelA ChannelB ChannelMessageQueue
答案 1 :(得分:0)
psubscribe已在IRedisSubscription接口下使用,那么我们可以使用 在C#中 subscription.SubscribeToChannelsMatching 只需按照您的需要像下面这样给它提供模式
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");
}
ActiveChannelName的值是类似于“ TestChannel *”的模式