public void Subscribe(string queueName, Action action) { Task.Factory.StartNew(() => { using (var client1 = new RedisClient("localhost")) { using (var subscription1 = client1.CreateSubscription()) { subscription1.OnSubscribe = channel => Debug.WriteLine(string.Format("Subscribed to '{0}'", channel)); subscription1.OnUnSubscribe = channel => Debug.WriteLine(string.Format("UnSubscribed from '{0}'", channel)); subscription1.OnMessage = (channel, msg) => { Debug.WriteLine(string.Format("Received '{0}' from channel '{1}' Busy: {2}", msg, channel, false)); action(); }; subscription1.SubscribeToChannels(queueName); Debug.WriteLine("Subscribed"); } } }).Start(); }
/// <summary> /// 订阅 /// </summary> public void Subscribe() { //创建订阅 IRedisSubscription subscription = client.CreateSubscription(); //接受到消息时 subscription.OnMessage += (channel, cmd) => { Console.WriteLine($"接受到消息时:消息通道:{channel},消息内容:{cmd}"); }; //订阅频道时 subscription.OnSubscribe = (channel) => { Console.WriteLine($"订阅客户端:开始订阅消息通道:{channel}"); }; //取消订阅频道时 subscription.OnUnSubscribe = (channel) => { Console.WriteLine($"订阅客户端:取消订阅消息通道:{channel}"); }; //订阅频道 subscription.SubscribeToChannels("channel1", "channel2"); }
static void ConsumeStream() { using (var redis = new RedisClient(Settings.AidrRedisConsumer_RedisHost, Settings.AidrRedisConsumer_RedisPort)) { using (var subscription = redis.CreateSubscription()) { subscription.OnSubscribe = (channel) => { Console.WriteLine("Subscribed to " + channel); }; subscription.OnUnSubscribe = (channel) => { Console.WriteLine("Unsubscribed from " + channel); }; subscription.OnMessage = (channel, message) => { if (message.EndsWith("}")) Console.Write("."); else Console.Write("!"); _tweetQueue.Enqueue(message); AllowAsyncDBWrite(); }; subscription.SubscribeToChannelsMatching(Settings.AidrRedisConsumer_SubscribePattern); } } }
static void Main(string[] args) { var conf = new RedisEndpoint() { Host = "xxxxxxxxxxxxxx.redis.cache.windows.net", Password = "******", Ssl = true, Port = 6380 }; using (IRedisClient client = new RedisClient(conf)) { IRedisSubscription sub = null; using (sub = client.CreateSubscription()) { sub.OnMessage += (channel, message) => { try { List<Item> items = JsonConvert.DeserializeObject<List<Item>>(message); Console.WriteLine((string)message); SignalRClass sc = new SignalRClass(); sc.SendRank(items); } catch (Exception ex) { Console.WriteLine(ex.Message); } }; } sub.SubscribeToChannels(new string[] { "Rank" }); } Console.ReadLine(); }
public void Run() { // 处理队列缓存的消息 ProcessQueueMessages(); using (var sub = _redis.CreateSubscription()) { sub.OnMessage = (channel, m) => { ProcessQueueMessages(); }; sub.SubscribeToChannels(_channels.Keys.ToArray()); } }
private static void ConsumerAction(object name) { using (var consumer = new RedisClient(RedisConStr)) { using (var subscription = consumer.CreateSubscription()) { subscription.OnSubscribe = (channel) => { Console.WriteLine("[{0}] Subscribe to channel '{1}'.", name, channel); }; subscription.OnUnSubscribe = (channel) => { Console.WriteLine("[{0}] Unsubscribe to channel '{1}'.", name, channel); }; subscription.OnMessage = (channel, message) => { Console.WriteLine("[{0}] Received message '{1}' from channel '{2}'.", name, message, channel); }; subscription.SubscribeToChannels("default"); } } }
static void Main(string[] args) { /*** String ***/ using (IRedisNativeClient client = new RedisClient()) { client.Set("urn:messages:1", Encoding.UTF8.GetBytes("Hello C# World!")); } using (IRedisNativeClient client = new RedisClient()) { var result = Encoding.UTF8.GetString(client.Get("urn:messages:1")); Console.WriteLine("Message: {0}", result); } /*** Lists ***/ using (IRedisClient client = new RedisClient()) { var customerNames = client.Lists["urn:customernames"]; customerNames.Clear(); customerNames.Add("Joe"); customerNames.Add("Mary"); customerNames.Add("Bob"); } using (IRedisClient client = new RedisClient()) { var customerNames = client.Lists["urn:customernames"]; foreach (var customerName in customerNames) { Console.WriteLine("Customer {0}", customerName); } } /*** Object ***/ long lastId = 0; using (IRedisClient client = new RedisClient()) { var customerClient = client.GetTypedClient<Customer>(); var customer = new Customer() { Id = customerClient.GetNextSequence(), Address = "123 Main St", Name = "Bob Green", Orders = new List<Order> { new Order { OrderNumber = "AB123"}, new Order { OrderNumber = "AB124"} } }; var storedCustomer = customerClient.Store(customer); lastId = storedCustomer.Id; } using (IRedisClient client = new RedisClient()) { var customerClient = client.GetTypedClient<Customer>(); var customer = customerClient.GetById(lastId); Console.WriteLine("Got customer {0}, with name {1}", customer.Id, customer.Name); } /*** Transaction ***/ using (IRedisClient client = new RedisClient()) { var transaction = client.CreateTransaction(); transaction.QueueCommand(c => c.Set("abc", 1)); transaction.QueueCommand(c => c.Increment("abc", 1)); transaction.Commit(); var result = client.Get<int>("abc"); Console.WriteLine(result); } /*** Publishing & Subscribing ***/ using (IRedisClient client = new RedisClient()) { client.PublishMessage("debug", "Hello C#!"); var sub = client.CreateSubscription(); sub.OnMessage = (c, m) => Console.WriteLine("Got message {0}, from channel {1}", m, c); sub.SubscribeToChannels("news"); } Console.ReadLine(); }
private void Subscribe() { try { var channelName = MachineSN; _Client = new RedisClient(_ip, _port); using (var subscription = _Client.CreateSubscription()) { subscription.OnSubscribe = channel => { }; subscription.OnUnSubscribe = channel => { }; subscription.OnMessage = (channel, msg) => { try { int index1 = msg.IndexOf(','); String Header = msg.Substring(1, index1 - 1); String[] HeaderArr = Header.Split(':'); if (HeaderArr.Length != 2) { return; } String ValueStr = HeaderArr[1].Trim('\"'); switch (ValueStr) { case "ToolBroken": { HncMessage <HNCAPI.Data.ToolBrokenInfo> tmes = Newtonsoft.Json.JsonConvert.DeserializeObject <HncMessage <HNCAPI.Data.ToolBrokenInfo> >(msg); if (OnToolBroken != null) { OnToolBroken(tmes.Body); } } break; case "Sample": { HncMessage <SampleMessge> SampleMsg = Newtonsoft.Json.JsonConvert.DeserializeObject <HncMessage <SampleMessge> >(msg); if (OnSubScribeSampleData != null) { OnSubScribeSampleData(SampleMsg.Body); } } break; case "HealthData": { HncMessage <HealthData> SampleMsg = Newtonsoft.Json.JsonConvert.DeserializeObject <HncMessage <HealthData> >(msg); if (OnHealthData != null) { OnHealthData(SampleMsg.Body); } break; } } } catch (Exception ex) { Console.WriteLine("Subscribe" + ex.Message); } }; subscription.SubscribeToChannels(channelName); //blocking } } catch (Exception ex) { Console.WriteLine("Subscribe" + ex.Message); this.Start(); } }