使用Service.Stack客户端编写redis pub sub的方法

pub相对简单

client.PublishMessage("channel", "msg");
 
sub有2种方法
方法1
var subscription = client.CreateSubscription();
subscription.OnMessage = (channel, msg) =>

{

    Console.WriteLine(String.Format("Received '{0}' from channel '{1}'", msg, channel));

};
subscription.SubscribeToChannels("testc");
运行完最后一句,就block主了,之后的代码都不会执行
 
 

方法2

var clientsManager = new BasicRedisClientManager();
var redisPubSub = new RedisPubSubServer(clientsManager, "testc")

{

    OnMessage = (channel, msg) => 

    { 

        "Received '{0}' from '{1}'".Print(msg, channel);

    } 

}.Start();

start会新开一个线程去监听

因为是新线程,所以用Console.Write是打印不出来的,得用service stack自己提供的print方法

你可能感兴趣的:(service)