|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text; |
| 5 | +using System.Threading.Tasks; |
| 6 | + |
| 7 | +using NATS.Client; |
| 8 | + |
| 9 | +// Create a new connection factory to create |
| 10 | +// a connection. |
| 11 | +Options opts = ConnectionFactory.GetDefaultOptions(); |
| 12 | +opts.Url = "nats://nats:4222"; |
| 13 | + |
| 14 | +// Creates a live connection to the default |
| 15 | +// NATS Server running locally |
| 16 | +ConnectionFactory cf = new ConnectionFactory(); |
| 17 | +IConnection c = cf.CreateConnection(opts); |
| 18 | + |
| 19 | +// Setup an event handler to process incoming messages. |
| 20 | +// An anonymous delegate function is used for brevity. |
| 21 | +EventHandler<MsgHandlerEventArgs> h = (sender, args) => |
| 22 | +{ |
| 23 | + // print the message |
| 24 | + Console.WriteLine(args.Message); |
| 25 | + |
| 26 | + // Here are some of the accessible properties from |
| 27 | + // the message: |
| 28 | + // args.Message.Data; |
| 29 | + // args.Message.Reply; |
| 30 | + // args.Message.Subject; |
| 31 | + // args.Message.ArrivalSubcription.Subject; |
| 32 | + // args.Message.ArrivalSubcription.QueuedMessageCount; |
| 33 | + // args.Message.ArrivalSubcription.Queue; |
| 34 | + |
| 35 | + // Unsubscribing from within the delegate function is supported. |
| 36 | + args.Message.ArrivalSubcription.Unsubscribe(); |
| 37 | +}; |
| 38 | + |
| 39 | +// The simple way to create an asynchronous subscriber |
| 40 | +// is to simply pass the event in. Messages will start |
| 41 | +// arriving immediately. |
| 42 | +IAsyncSubscription s = c.SubscribeAsync("foo", h); |
| 43 | + |
| 44 | +// Alternatively, create an asynchronous subscriber on subject foo, |
| 45 | +// assign a message handler, then start the subscriber. When |
| 46 | +// multicasting delegates, this allows all message handlers |
| 47 | +// to be setup before messages start arriving. |
| 48 | +IAsyncSubscription sAsync = c.SubscribeAsync("foo"); |
| 49 | +sAsync.MessageHandler += h; |
| 50 | +sAsync.Start(); |
| 51 | + |
| 52 | +// Simple synchronous subscriber |
| 53 | +ISyncSubscription sSync = c.SubscribeSync("foo"); |
| 54 | + |
| 55 | +// Using a synchronous subscriber, gets the first message available, |
| 56 | +// waiting up to 1000 milliseconds (1 second) |
| 57 | +Msg m = sSync.NextMessage(1000); |
| 58 | + |
| 59 | +c.Publish("foo", Encoding.UTF8.GetBytes("hello world")); |
| 60 | + |
| 61 | +// Unsubscribing |
| 62 | +sAsync.Unsubscribe(); |
| 63 | + |
| 64 | +// Publish requests to the given reply subject: |
| 65 | +c.Publish("foo", "bar", Encoding.UTF8.GetBytes("help!")); |
| 66 | + |
| 67 | +// Sends a request (internally creates an inbox) and Auto-Unsubscribe the |
| 68 | +// internal subscriber, which means that the subscriber is unsubscribed |
| 69 | +// when receiving the first response from potentially many repliers. |
| 70 | +// This call will wait for the reply for up to 1000 milliseconds (1 second). |
| 71 | +m = c.Request("foo", Encoding.UTF8.GetBytes("help"), 1000); |
| 72 | + |
| 73 | +// Draining and closing a connection |
| 74 | +c.Drain(); |
| 75 | + |
| 76 | +// Closing a connection |
| 77 | +c.Close(); |
0 commit comments