public void ThroughXPubXSubWithReconnectingPublisher() { using (var xpub = new XPublisherSocket()) using (var xsub = new XSubscriberSocket()) using (var poller = new NetMQPoller { xsub, xpub }) { var xPubPort = (ushort)xpub.BindRandomPort("tcp://*"); var xSubPort = (ushort)xsub.BindRandomPort("tcp://*"); var proxy = new Proxy(xsub, xpub, poller: poller); proxy.Start(); poller.RunAsync(); // long running subscriber using (var sub = new SubscriberSocket()) { sub.Connect(string.Format("tcp://localhost:{0}", xPubPort)); sub.Subscribe("A"); // publisher 1 using (var pub = new PublisherSocket()) { pub.Connect(string.Format("tcp://localhost:{0}", xSubPort)); // give the publisher a chance to learn of the subscription Thread.Sleep(100); pub.SendMoreFrame("A").SendFrame("1"); } // publisher 2 using (var pub = new PublisherSocket()) { pub.Connect(string.Format("tcp://localhost:{0}", xSubPort)); // give the publisher a chance to learn of the subscription Thread.Sleep(100); pub.SendMoreFrame("A").SendFrame("2"); } var frames = new List <string>(); Assert.True(sub.TryReceiveMultipartStrings(TimeSpan.FromSeconds(1), ref frames)); CollectionAssert.AreEqual(new[] { "A", "1" }, frames); Assert.True(sub.TryReceiveMultipartStrings(TimeSpan.FromSeconds(1), ref frames)); CollectionAssert.AreEqual(new[] { "A", "2" }, frames); } } }
/// <summary> /// Listens for all topics on the given ZMQ endpoint /// /// Intended to be run as in a Thread: /// _subscriberThread = new Thread(NetMqSubscriber); /// _subscriberThread.Start("tcp://127.0.0.1:13337"); /// </summary> /// <param name="param">The ZMQ-endpoint as a string</param> private void NetMqSubscriber(object param) { AsyncIO.ForceDotNet.Force(); var endpoint = (string)param; Debug.Log("Creating subscriber"); var timeout = new TimeSpan(0, 0, 0, 1); //1 s try { subSocket = new SubscriberSocket(); subSocket.Options.Linger = TimeSpan.Zero; subSocket.Subscribe("volume_extremes"); subSocket.Connect(endpoint); Debug.Log("Sub connected"); var msg = new List <string>(); while (running) { if (!subSocket.TryReceiveMultipartStrings(new TimeSpan(0, 0, 0, 0, 300), ref msg)) { continue; } if (msg.Count < 2) { Debug.LogError(msg); continue; } Debug.Log("Got BBox message: " + msg[1]); bbox = JsonConvert.DeserializeObject <InvBoundingBox>(msg[1]); dirty = true; } } catch (Exception ex) { Debug.Log("Got exception: " + ex); } finally { try { subSocket.Close(); } catch (ObjectDisposedException e) { Debug.LogWarning("Tried to close socket but was already disposed"); } } Debug.Log("Subscriber has the deds"); }
public void ThroughXPubXSub() { using (var xpub = new XPublisherSocket()) using (var xsub = new XSubscriberSocket()) using (var proxyPoller = new NetMQPoller { xsub, xpub }) { var xPubPort = (ushort)xpub.BindRandomPort("tcp://*"); var xSubPort = (ushort)xsub.BindRandomPort("tcp://*"); var proxy = new Proxy(xsub, xpub, poller: proxyPoller); proxy.Start(); proxyPoller.RunAsync(); using (var pub = new PublisherSocket()) using (var sub = new SubscriberSocket()) { // Client 1 sub.Connect(string.Format("tcp://localhost:{0}", xPubPort)); pub.Connect(string.Format("tcp://localhost:{0}", xSubPort)); sub.Subscribe("A"); // Client 2 Thread.Sleep(500); pub.SendMoreFrame("A").SendFrame("Hello"); var frames = new List <string>(); Assert.True(sub.TryReceiveMultipartStrings(TimeSpan.FromSeconds(1), ref frames)); CollectionAssert.AreEqual( new[] { "A", "Hello" }, frames); } } }