Last active
February 5, 2022 14:40
-
-
Save alexandrebl/e45d142c44cfaf8eb7b7bc3a6fb3f4b3 to your computer and use it in GitHub Desktop.
NATS.io.Subject.Based.Messaging.Subscriber
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; | |
| namespace NatsSub1 | |
| { | |
| internal class Program | |
| { | |
| static void Main(string[] args) | |
| { | |
| // Define variáveis | |
| var subscrption = GetSubscription(args); | |
| // Obtem a conexão | |
| var connection = GetConnection(); | |
| // Assinatura | |
| var sAsync = connection.SubscribeAsync(subscrption); | |
| // Processamento do evento a cada mensagem recebida | |
| sAsync.MessageHandler += (sender, args) => | |
| { | |
| Console.WriteLine($"{DateTime.Now:F} - Received: {args.Message}"); | |
| }; | |
| // Inicia a leitura da assinatura | |
| sAsync.Start(); | |
| // Aguarda uma tecla ser pressionada | |
| Console.ReadLine(); | |
| // Desconectando | |
| sAsync.Unsubscribe(); | |
| connection.Drain(); | |
| connection.Close(); | |
| } | |
| /// <summary> | |
| /// Obtem a assinatura | |
| /// </summary> | |
| /// <param name="args">argumentos de linha de comando</param> | |
| /// <returns>assinatura</returns> | |
| public static string GetSubscription(String[] args) => args[0]; | |
| /// <summary> | |
| /// Obtem uma nova conexão | |
| /// </summary> | |
| /// <returns>nova conexão com NATs.io</returns> | |
| public static IConnection GetConnection() | |
| { | |
| var cf = new ConnectionFactory(); | |
| var c = cf.CreateConnection(); | |
| return c; | |
| } | |
| } | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
natssub "subject"