Exemple #1
0
        public static async Task Main(string[] args)
        {
            var cnInfo = new ConnectionInfo("localhost");

            _client = new NatsClient(cnInfo);

            await _client.ConnectAsync();

            _client.Sub("getTemp", stream => stream.Subscribe(msg =>
            {
                var parts = msg.GetPayloadAsString().Split('@');
                _client.Pub(msg.ReplyTo, $"Temp is {TempService.Get(parts[0], parts[1])}C");
            }));

            while (true)
            {
                Console.WriteLine("Query? (y=yes;n=no)");
                if (Console.ReadKey().KeyChar == 'n')
                {
                    break;
                }

                Console.WriteLine();

                Console.WriteLine($"Got reply: {_client.RequestAsync("getTemp", "STOCKHOLM@SWEDEN").Result.GetPayloadAsString()}");
            }

            _client.Disconnect();
        }
        public static void PubAsJson <TItem>(this INatsClient client, string subject, TItem item, string replyTo = null) where TItem : class
        {
            var payload = JsonEncoding.Default.Encode(item);

            client.Pub(subject, payload, replyTo);
        }