public void MultipleSubscribersOnDifferentTopics() { using (var pub = new PublisherSocket()) using (var sub1 = new SubscriberSocket()) using (var sub2 = new SubscriberSocket()) { var port = pub.BindRandomPort("tcp://127.0.0.1"); sub1.Connect("tcp://127.0.0.1:" + port); sub2.Connect("tcp://127.0.0.1:" + port); sub1.Subscribe("1"); sub1.Subscribe("1&2"); sub2.Subscribe("2"); sub2.Subscribe("1&2"); Thread.Sleep(500); pub.SendMoreFrame("1").SendFrame("A"); CollectionAssert.AreEqual(new[] { "1", "A" }, sub1.ReceiveMultipartStrings()); Assert.IsFalse(sub2.TrySkipFrame()); pub.SendMoreFrame("2").SendFrame("B"); Assert.IsFalse(sub1.TrySkipFrame()); CollectionAssert.AreEqual(new[] { "2", "B" }, sub2.ReceiveMultipartStrings()); pub.SendMoreFrame("1&2").SendFrame("C"); CollectionAssert.AreEqual(new[] { "1&2", "C" }, sub1.ReceiveMultipartStrings()); CollectionAssert.AreEqual(new[] { "1&2", "C" }, sub2.ReceiveMultipartStrings()); } }
public void Unsubscribe() { using (var pub = new PublisherSocket()) using (var sub = new SubscriberSocket()) { int port = pub.BindRandomPort("tcp://127.0.0.1"); sub.Connect("tcp://127.0.0.1:" + port); sub.Subscribe("A"); // let the subscriber connect to the publisher before sending a message Thread.Sleep(500); pub.SendMoreFrame("A").SendFrame("Hello"); CollectionAssert.AreEqual(new[] { "A", "Hello" }, sub.ReceiveMultipartStrings()); sub.Unsubscribe("A"); Thread.Sleep(500); pub.SendMoreFrame("A").SendFrame("Hello again"); Assert.IsFalse(sub.TrySkipFrame()); } }
private static void Main() { Console.Title = "NetMQ HelloWorld"; using (var sub = new SubscriberSocket()) { sub.Options.PlainUsername = "******"; sub.Options.PlainPassword = "******"; sub.Connect("tcp://192.168.0.52:6021"); sub.Subscribe("CraneMetrics:"); while (true) { List <string> messageList = sub.ReceiveMultipartStrings(); Console.WriteLine("Topic: {0} Message: {1}", messageList[0], messageList[1]); } } }
public void SimplePubSub() { using (var pub = new PublisherSocket()) using (var sub = new SubscriberSocket()) { var port = pub.BindRandomPort("tcp://127.0.0.1"); sub.Connect("tcp://127.0.0.1:" + port); sub.Subscribe(""); // let the subscriber connect to the publisher before sending a message Thread.Sleep(500); // Send the topic only pub.SendFrame("A"); CollectionAssert.AreEqual( new[] { "A" }, sub.ReceiveMultipartStrings()); } }
void SubWorker() { running = true; while (running) { List <string> data = sub.ReceiveMultipartStrings(); if (data.Count == 3) { string subscriptionName = data[0]; string sType = data[1]; string json = data[2]; //Console.WriteLine ( "{0} : {1}", subscriptionName, sType); Tuple <string, string> key = new Tuple <string, string> (subscriptionName, sType); typehelper th; if (dictTypes.TryGetValue(key, out th)) { var o = JsonConvert.DeserializeObject(json, th.t); th.methodInfo.Invoke(th.target, new object[] { o }); } } } }
public void MultiplePublishersAndSubscribersOnSameTopic() { using (var pub1 = new PublisherSocket()) using (var pub2 = new PublisherSocket()) using (var sub1 = new SubscriberSocket()) using (var sub2 = new SubscriberSocket()) { int port1 = pub1.BindRandomPort("tcp://127.0.0.1"); int port2 = pub2.BindRandomPort("tcp://127.0.0.1"); sub1.Connect("tcp://127.0.0.1:" + port1); sub1.Connect("tcp://127.0.0.1:" + port2); sub2.Connect("tcp://127.0.0.1:" + port1); sub2.Connect("tcp://127.0.0.1:" + port2); // should subscribe to both sub1.Subscribe("A"); sub2.Subscribe("A"); Thread.Sleep(500); // Send from pub 1 pub1.SendMoreFrame("A").SendFrame("Hello from the first publisher"); CollectionAssert.AreEqual(new[] { "A", "Hello from the first publisher" }, sub1.ReceiveMultipartStrings()); CollectionAssert.AreEqual(new[] { "A", "Hello from the first publisher" }, sub2.ReceiveMultipartStrings()); // Send from pub 2 pub2.SendMoreFrame("A").SendFrame("Hello from the second publisher"); CollectionAssert.AreEqual(new[] { "A", "Hello from the second publisher" }, sub1.ReceiveMultipartStrings()); CollectionAssert.AreEqual(new[] { "A", "Hello from the second publisher" }, sub2.ReceiveMultipartStrings()); } }