コード例 #1
0
        public static Task RunAsync(ConnectionInfo cnInfo)
        {
            using (var client = new NatsClient("Publisher", cnInfo))
            {
                client.Connect();

                while (true)
                {
                    Console.WriteLine("Say what? (blank=quit)");
                    var message = Console.ReadLine();
                    if (string.IsNullOrWhiteSpace(message))
                    {
                        break;
                    }

                    int n;
                    Console.WriteLine("How many? (blank=quit)");
                    if (!int.TryParse(Console.ReadLine(), out n))
                    {
                        break;
                    }

                    client.PubMany(p =>
                    {
                        for (var c = 0; c < n; c++)
                        {
                            p.Pub("demo", message);
                        }
                    });
                }
            }

            return(Task.CompletedTask);
        }
コード例 #2
0
        public async Task Client_Should_be_able_to_publish_and_consume_messages_When_publishing_batch()
        {
            var subject  = Context.GenerateSubject();
            var messages = new[]
            {
                "My test string\r\nwith two lines and\ttabs!",
                "Foo bar!",
                "My async test string\r\nwith two lines and\ttabs!",
                "Async Foo bar!"
            };

            _sync    = Sync.Max(4);
            _client1 = await Context.ConnectClientAsync();

            _client1.MsgOpStream.Subscribe(msg => _sync.Release(msg));
            _client1.Sub(subject);

            await Context.DelayAsync();

            _client1.PubMany(async p =>
            {
                p.Pub(subject, messages[0]);
                p.Pub(subject, Encoding.UTF8.GetBytes(messages[1]));
                await p.PubAsync(subject, messages[2]);
                await p.PubAsync(subject, Encoding.UTF8.GetBytes(messages[3]));
            });

            _sync.WaitForAll();
            _sync.InterceptedCount.Should().Be(messages.Length);
            _sync.Intercepted.Select(m => m.GetPayloadAsString()).ToArray().Should().Contain(messages);
        }
コード例 #3
0
        public void Client_Should_be_able_to_publish_and_consume_messages_When_publishing_batch()
        {
            var interceptCount = 0;
            var intercepted    = new List <MsgOp>();
            var messages       = new[]
            {
                "My test string\r\nwith two lines and\ttabs!",
                "Foo bar!",
                "My async test string\r\nwith two lines and\ttabs!",
                "Async Foo bar!"
            };

            _client1.MsgOpStream.Subscribe(msg =>
            {
                intercepted.Add(msg);
                var x = Interlocked.Increment(ref interceptCount);
                if (x == messages.Length)
                {
                    ReleaseOne();
                }
            });
            _client1.Sub("Test");

            _client1.PubMany(async p =>
            {
                p.Pub("Test", messages[0]);
                p.Pub("Test", Encoding.UTF8.GetBytes(messages[1]));
                await p.PubAsync("Test", messages[2]);
                await p.PubAsync("Test", Encoding.UTF8.GetBytes(messages[3]));
            });

            WaitOne();
            intercepted.Should().HaveCount(messages.Length);
            intercepted.Select(m => m.GetPayloadAsString()).ToArray().Should().Contain(messages);
        }