Created
February 8, 2022 13:29
-
-
Save alexandrebl/14fa6eb249fbf0d43a47d3f82d0bb4a0 to your computer and use it in GitHub Desktop.
NATS.io.RequestReply.pub.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| using NATS.Client; | |
| using System; | |
| using System.Text; | |
| using System.Text.Json; | |
| using System.Threading; | |
| namespace NatsPub1 | |
| { | |
| internal class Program | |
| { | |
| static void Main(string[] args) | |
| { | |
| //conexão | |
| var connectionFactory = new ConnectionFactory(); | |
| var connection = connectionFactory.CreateConnection(); | |
| var subscription = nameof(Order); | |
| var random = new Random(DateTime.Now.Millisecond); | |
| var index = 1U; | |
| while (true) | |
| { | |
| var requestOrder = new Order(index, (ulong)random.Next(1, 20000)); | |
| var message = JsonSerializer.Serialize(requestOrder); | |
| Console.ForegroundColor = ConsoleColor.Cyan; | |
| Console.WriteLine($"{DateTime.Now:F} - Send: {message}"); | |
| var responseData = connection.Request( | |
| subscription, Encoding.UTF8.GetBytes(message)); | |
| var receivedOrder = JsonSerializer.Deserialize<Order>(responseData.Data); | |
| Console.ForegroundColor = receivedOrder.OrderStatus == OrderStatus.approved | |
| ? ConsoleColor.Green : ConsoleColor.Red; | |
| Console.WriteLine($"Received Order / Id: {receivedOrder.Id}" | |
| + $"/ Amount: {receivedOrder.Amount:c} / Status: Id: {receivedOrder.OrderStatus}"); | |
| Console.ForegroundColor = ConsoleColor.Yellow; | |
| Console.WriteLine("----------------------"); | |
| index++; | |
| Thread.Sleep(1000); | |
| } | |
| } | |
| } | |
| public enum OrderStatus { pending, approved, canceled } | |
| public class Order | |
| { | |
| public ulong Id { get; set; } | |
| public ulong Amount { get; set; } | |
| public OrderStatus OrderStatus { get; set; } | |
| public Order(ulong id, ulong amount) | |
| { | |
| Amount = amount; | |
| Id = id; | |
| OrderStatus = OrderStatus.pending; | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment