/// <summary> /// 在 Rabbit 服务器中添加一个订阅方法。 /// </summary> /// <param name="subjectType">主题的类型。</param> /// <param name="subscriber">读取主题的方法。</param> public void AddSubscriber(Type subjectType, Delegate subscriber) { var channelName = ChannelHelper.GetChannelName(subjectType); var list = subscribers.GetOrAdd(channelName, () => new RabbitChannelCollection()); list.Add(new RabbitChannel(subscriber, StartQueue(channelName))); }
/// <summary> /// 在 Redis 服务器中添加一个订阅方法。 /// </summary> /// <param name="subjectType">主题的类型。</param> /// <param name="subscriber">读取主题的方法。</param> public void AddSubscriber(Type subjectType, Delegate subscriber) { var client = GetConnection(); var channelName = ChannelHelper.GetChannelName(subjectType); #if NETSTANDARD client.Subscribe((channelName, msg => { var subject = Deserialize(subjectType, msg.Body); if (subject != null) { subscriber.DynamicInvoke(subject); } } )); #else client.GetSubscriber().Subscribe(channelName, (channel, value) => { var subject = Deserialize(subjectType, Encoding.UTF8.GetString(value)); if (subject != null) { subscriber.DynamicInvoke(subject); } }); #endif }
/// <summary> /// 向 Rabbit 服务器发送消息主题。 /// </summary> /// <typeparam name="TSubject"></typeparam> /// <param name="subject">主题内容。</param> public void Publish <TSubject>(TSubject subject) where TSubject : class { var channelName = ChannelHelper.GetChannelName(typeof(TSubject)); var data = Serialize(subject); Publish(channelName, data); }
/// <summary> /// 移除相关的订阅方法。 /// </summary> /// <param name="subjectType">主题的类型。</param> public void RemoveSubscriber(Type subjectType) { var client = GetConnection(); var channelName = ChannelHelper.GetChannelName(subjectType); RemoveSubscriber(channelName); }
/// <summary> /// 移除相关的订阅方法。 /// </summary> /// <param name="subjectType">主题的类型。</param> public void RemoveSubscriber(Type subjectType) { var client = GetConnection(); var channelName = ChannelHelper.GetChannelName(subjectType); #if NETSTANDARD #else client.GetSubscriber().Unsubscribe(channelName); #endif }
/// <summary> /// 在 Rabbit 服务器中添加一个订阅方法。 /// </summary> /// <param name="subjectType">主题的类型。</param> /// <param name="subscriber">读取主题的方法。</param> public void AddSubscriber(Type subjectType, Delegate subscriber) { var channelName = ChannelHelper.GetChannelName(subjectType); var list = subscribers.GetOrAdd(channelName, k => { StartQueue(channelName); return(new List <Delegate>()); }); list.Add(subscriber); }
/// <summary> /// 向 Redis 服务器发送消息主题。 /// </summary> /// <typeparam name="TSubject"></typeparam> /// <param name="subject">主题内容。</param> public void Publish <TSubject>(TSubject subject) where TSubject : class { #if NETSTANDARD var client = GetConnection(); var channelName = ChannelHelper.GetChannelName(typeof(TSubject)); client.Publish(channelName, Serialize(subject)); #else var data = Encoding.UTF8.GetBytes(Serialize(subject)); var channelName = ChannelHelper.GetChannelName(typeof(TSubject)); Publish(channelName, data); #endif }
/// <summary> /// 在 Redis 服务器中添加一个订阅方法。 /// </summary> /// <typeparam name="TSubject"></typeparam> /// <param name="subscriber">读取主题的方法。</param> public void AddSubscriber <TSubject>(Action <TSubject> subscriber) where TSubject : class { var client = GetConnection(); var channelName = ChannelHelper.GetChannelName(typeof(TSubject)); #if NETSTANDARD client.Subscribe((channelName, msg => { var subject = Deserialize <TSubject>(msg.Body); subscriber(subject); } )); #else client.GetSubscriber().Subscribe(channelName, (channel, value) => { var subject = Deserialize <TSubject>(Encoding.UTF8.GetString(value)); subscriber(subject); }); #endif }
public string GetChannelText(string id) { return(ChannelHelper.GetChannelName(ArticleHelper.GetArticle(id).OwnerID)); }