Ejemplo n.º 1
0
        public async Task <Message> PublishMessageToTopic(Message msg)
        {
            NatsClient <Message> client = new NatsClient <Message>();

            await Task.Run(() => client.Publish(TopicName, msg));

            return(msg);
        }
Ejemplo n.º 2
0
        public async Task PublishTest()
        {
            const string subject            = "some_subject";
            const string message            = "some message";
            var          size               = Encoding.UTF8.GetByteCount(message);
            var          expectedData       = Encoding.UTF8.GetBytes($"PUB {subject} {size}\r\n{message}\r\n");
            var          mockNatsConnection = new Mock <INatsConnection>();

            mockNatsConnection.Setup(x => x.OnConnect).Returns(Observable.Return(new ServerInfo()));

            using (var client = new NatsClient(mockNatsConnection.Object))
            {
                await client.Publish(subject, message);
            }

            mockNatsConnection.Verify(x =>
                                      x.Write(It.Is <byte[]>(data => expectedData.SequenceEqual(data)), It.IsAny <CancellationToken>()));
        }
Ejemplo n.º 3
0
        static async Task Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            // hit ctrl-C to close/exit

            var cts = new CancellationTokenSource();

            Console.CancelKeyPress += (_, e) =>
            {
                Console.WriteLine("cancelled...");
                cts.Cancel();
                e.Cancel = true;
            };

            var sp = ConfigureServices();

            var nats = new NatsClient();
            await nats.StartAsync(new IPEndPoint(IPAddress.Loopback, 4222), sp);

            nats.Connect(new ConnectOperation {
                Verbose = false
            });

            nats.Subscribe("test", msg =>
            {
                var text = Encoding.UTF8.GetString(msg.Data.Span);
                Console.WriteLine($"OnMsg: subject:{msg.Subject} sid:{msg.Sid} replyto:{msg.ReplyTo} text:{text}");
            });

            while (!cts.Token.IsCancellationRequested)
            {
                Console.WriteLine("pub...");
                nats.Publish("test", Encoding.UTF8.GetBytes("hello"));
                await Task.Delay(2000);
            }

            Console.WriteLine("done...");
            Console.ReadLine();
        }
Ejemplo n.º 4
0
 public static async Task Publish(this NatsClient @this, string subject, string data, CancellationToken cancellationToken = default(CancellationToken))
 {
     await @this.Publish(subject, Encoding.UTF8.GetBytes(data), cancellationToken);
 }
 public void SendLogyardNotification(string logyardId, string message)
 {
     NatsClient.Publish(string.Format("logyard.{0}.newinstance", logyardId), null, message);
 }
 /// <summary>
 /// Sends the Staging advertise message.
 /// </summary>
 /// <param name="message">The message.</param>
 public void SendStagingAdvertise(string message)
 {
     NatsClient.Publish("staging.advertise", null, message);
 }
 /// <summary>
 /// Sends the DEA advertise message for centralized apps placement.
 /// </summary>
 /// <param name="message">The message.</param>
 public void SendDeaAdvertise(string message)
 {
     NatsClient.Publish("dea.advertise", null, message);
 }
 /// <summary>
 /// Sends the router unregister to the message bus.
 /// </summary>
 /// <param name="message">The message.</param>
 public void SendRouterUnregister(string message)
 {
     NatsClient.Publish("router.unregister", null, message);
 }
 /// <summary>
 /// Sends the droplet exited to the message bus.
 /// </summary>
 /// <param name="message">The message.</param>
 public void SendDropletExited(string message)
 {
     NatsClient.Publish("droplet.exited", null, message);
     Logger.Debug(Strings.SentDropletExited, message);
 }
 /// <summary>
 /// Sends the DEA start to the message bus.
 /// </summary>
 /// <param name="message">The message.</param>
 public void SendDeaStart(string message)
 {
     NatsClient.Publish("dea.start", null, message);
 }
 /// <summary>
 /// Sends the DEA heartbeat to the message bus.
 /// </summary>
 /// <param name="message">The message.</param>
 public void SendDeaHeartbeat(string message)
 {
     NatsClient.Publish("dea.heartbeat", null, message);
 }