码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2014-12-02 18:53:41      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:style   io   ar   color   使用   sp   for   on   art   

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.Stack客户端编写redis pub sub的方法

标签:style   io   ar   color   使用   sp   for   on   art   

原文地址:http://www.cnblogs.com/czcz1024/p/4137957.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!