/// <summary> /// Publishes a message to Nats /// </summary> /// <param name="message"></param> public void Publish(string message) { if (!_natsClient.IsConnected) { _natsClient.Connect(); } // push message to exchange _natsClient.PubAsJsonAsync(_subject, message).ConfigureAwait(false); }
public static void Main(string[] args) { var cnInfo = new ConnectionInfo("localhost") { Credentials = new Credentials("test", "p@ssword123") }; _client = new NatsClient(cnInfo); _client.Connect(); _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"); })); var c = 0; while (true) { Console.WriteLine("Query? (y=yes;n=no)"); if (Console.ReadKey().KeyChar == 'n') { break; } Console.WriteLine(); c++; Console.WriteLine($"Got reply: {_client.RequestAsync("getTemp", "STOCKHOLM@SWEDEN").Result.GetPayloadAsString()}"); if (c % 5 == 0) { _client.Disconnect(); _client.Connect(); } } _client.Disconnect(); }