【问题标题】:How to unsubscribe from a channel using "New Managed Pub/Sub Server" in Servicestack.Redis如何使用 Servicestack.Redis 中的“New Managed Pub/Sub Server”取消订阅频道
【发布时间】:2015-12-22 23:39:12
【问题描述】:

New Managed Pub/Sub Server 文档中,他们展示了如何在 pubsubserver 的初始化中订阅频道。但是,如果需要,您如何在节目后期取消订阅特定频道?

我的代码:

using ServiceStack.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;

namespace va.communication
{
    class Program
    {
        static void Main(string[] args)
        {
            var clientsManager = new PooledRedisClientManager();

            //subscribe to channels 'messages' and 'queue'
            var redisPubSub = new RedisPubSubServer(clientsManager, "messages","queue")
            {
                OnUnSubscribe = (channel) =>
                {
                    Console.WriteLine("Unsubscribed from channel '{0}'", channel);
                },
                OnMessage = (channel, msg) =>
                {
                    Console.WriteLine("Received '{0}' from channel '{1}'", msg, channel);
                }
            };
            redisPubSub.Start();

            //...
            //other code which takes ~5s after which i want to unsubscribe from 'messages'
            Thread.Sleep(5000); 
            //...

            //no longer require channel 'messages'. what to do here?

            while (true) ;//continue with other code
        }
    }
}

【问题讨论】:

    标签: c# redis servicestack servicestack.redis


    【解决方案1】:

    调用 Stop() 或 Dispose() 将取消订阅并停止收听订阅的频道。如果您想稍后再次开始收听,请使用 Stop();如果您已完成 RedisPubSubManager,请使用 Dispose()。

    【讨论】:

    • Stop() 对我不起作用。能否给个示例代码的链接?
    • @SagarShah RedisMqServer 和 RedisServerEvents 类使用它。不知道为什么要在另一个线程中启动它,它已经在后台线程中运行,初始化后也不要管通道。
    【解决方案2】:

    感谢mythz 和一些对我有用的研究:

    要取消订阅“消息”,请使用这个->

    //reassign channels to just 'queue' removing 'messages' as a channel
    redisPubSub.Channels = new string[] {"queue" };
    

    取消订阅所有频道使用这个->

    redisPubSub.Channels = new string[] { };//empty channel array
    

    【讨论】:

      猜你喜欢
      • 2011-07-15
      • 2018-04-11
      • 1970-01-01
      • 2018-11-13
      • 2022-07-27
      • 1970-01-01
      • 2022-12-02
      • 2019-06-09
      相关资源
      最近更新 更多