Redis-EasyRedisMq-将消息发布到单个订户,但接收所有订户

时间:2018-07-18 16:31:10

标签: c# redis

我正在使用EasyRedisMQ作为消息队列。它可以很好地用于MQ目的,但我也想将EasyRedisMQ用于Chat。 问题->当发布者正在向所有订阅者发布消息时,但是我只想发布给单个订阅者。

      // redis config 
        var redisConfiguration = new RedisConfiguration()
        {
            AbortOnConnectFail = true,
            KeyPrefix = "_my_key_prefix_",
            Hosts = new RedisHost[]
            {
                new RedisHost(){Host = "localhost", Port = 6379},
            },
            AllowAdmin = true,
            ConnectTimeout = 3000,
            Database = 1,
            ServerEnumerationStrategy = new ServerEnumerationStrategy()
            {
                Mode = ServerEnumerationStrategy.ModeOptions.All,
                TargetRole = ServerEnumerationStrategy.TargetRoleOptions.Any,
                UnreachableServerAction = ServerEnumerationStrategy.UnreachableServerActionOptions.Throw
            }
        };

    // simple injector 
        var container = new Container();
        RegisterAllMq(container);
        container.Register<ISerializer>(()=> new NewtonsoftSerializer(new JsonSerializerSettings(){Culture = CultureInfo.CurrentCulture}));

        //ISerializer searilizer =;
        container.Register<ICacheClient>(
            () => new StackExchangeRedisCacheClient(container.GetInstance<ISerializer>(), redisConfiguration), Lifestyle.Singleton);


        var messageBroker = container.GetInstance<IMessageBroker>();

        //The subscriber has it's own id (like login id)
        //This consumer will get its own queue and it gets its own copy of every message published.
        Console.WriteLine("Enter you user id");
        var id = Console.ReadLine();
        var subscriber = messageBroker.SubscribeAsync<ConsoleMessage>(id, async x => { await WriteConsoleMessageAsync(x); });

//订阅了此ID。当打开多个控制台时,每个客户端将具有不同的ID。例如控制台ID 1,控制台ID 2 ...

        Console.WriteLine($"{id} ==> is listening for messages. Ctrl+C to quit.");

//这正在处理消息的发布,此代码也是同一控制台应用程序的一部分。它正在将消息发布到所有控制台1,2,3。             而(真)             {                 Console.WriteLine(“输入要发布的消息。按Ctrl + C退出。”);                 var message = Console.ReadLine();

            messageBroker.PublishAsync<ConsoleMessage>(new ConsoleMessage
            {
                Message = message
            });
            Console.WriteLine("Message published successfully.");
        }

但是我想实现,就像一个2 1聊天应用程序。 订户“ A”将输入他的id(111),他还将提供另一个订户“ B”的id(222)(他要向其发送消息)。因此,当订户“ A”发送消息时,它仅应发布到订户“ B”。

在此示例中,我知道我没有输入订户“ B” id选项的选项。

目标是使用redis与纯c#聊天(无Angular,无套接字)

0 个答案:

没有答案