gpt4 book ai didi

c# - 如何在 Servicestack.Redis 中使用 "New Managed Pub/Sub Server"取消订阅 channel

转载 作者:可可西里 更新时间:2023-11-01 11:21:00 25 4
gpt4 key购买 nike

New Managed Pub/Sub Server他们的文档展示了如何在 pubsubserver 的初始化中订阅 channel 。但是,您如何在程序稍后需要时取消订阅特定 channel ?

我的代码:

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
}
}
}

最佳答案

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

关于c# - 如何在 Servicestack.Redis 中使用 "New Managed Pub/Sub Server"取消订阅 channel ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32777087/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com