简体   繁体   English

谁能告诉我这个ZeroMQ代码有什么问题吗?

[英]Can anyone tell me what is wrong with this ZeroMQ code?

I have tried a variety of different things but this is essentially what I am trying to do. 我尝试了各种不同的方法,但这实际上是我要尝试的方法。 Have an in process pubsub set up. 设置了正在处理的pubsub。 I reran this code and I am getting a connection refused on the attempt to connect a pub socket to the front end of the forwarder device. 我重新运行了此代码,但在尝试将pub套接字连接到转发器设备的前端时遇到连接被拒绝的情况。

Here is the code 这是代码

string expectedAddress = "XXXX";
        string message = "hello its me";
        int count = 0;
        using (var context = ZmqContext.Create())
        {
            using (var forwarderDevice = new ForwarderDevice(context, "inproc://front", "inproc://back", DeviceMode.Threaded))
            {
                using (var pub = Helper.GetConnectedPublishSocket(context, "inproc://front"))
                {
                    using (var sub = Helper.GetConnectedSubscribeSocket(context, "inproc://back")) 
                    {
                        forwarderDevice.Start();

                        Helper.SendOneSimpleMessage(expectedAddress, message, pub);

                        var zmqMessage = Helper.ReceiveMessage(sub);

                        Assert.AreEqual(count, zmqMessage.FrameCount);
                        Frame frame = zmqMessage[0];
                        var address = Encoding.Unicode.GetString(frame.Buffer);
                        Assert.AreEqual(expectedAddress, address);
                    }
                }
                forwarderDevice.Stop();
            }
        }

Ok First thing I know I am doing wrong... the start should come directly after the creation of the device, before you attempt to connect to it. 好,第一件事,我知道我做错了...开始应在创建设备后立即进行,然后再尝试连接它。 Second thing is that the device is started on another thread and may not be ready by the time the main thread attempts to connect so you need to block some how until it is ready. 第二件事是设备在另一个线程上启动,并且在主线程尝试连接时可能尚未就绪,因此您需要阻止一些方法,直到就绪为止。 Code now looks like: 现在的代码如下所示:

string expectedAddress = "XXXX";
        string message = "hello its me";
        int count = 0;
        using (var context = ZmqContext.Create())
        {
            using (var forwarderDevice = new ForwarderDevice(context, "inproc://front", "inproc://back", DeviceMode.Threaded))
            {
                forwarderDevice.Start();
                while (!forwarderDevice.IsRunning)
                { }

                using (var pub = Helper.GetConnectedPublishSocket(context, "inproc://front"))
                {
                    using (var sub = Helper.GetConnectedSubscribeSocket(context, "inproc://back")) 
                    {
                        Task<ZmqMessage> task = Task.Run(() =>
                            {
                                var zmqMessage = Helper.ReceiveMessage(sub);
                                return zmqMessage;
                            });

                        Helper.SendOneSimpleMessage(expectedAddress, message, pub);
                        task.Wait();

                        var result = task.Result;

                        Assert.AreEqual(count, result.FrameCount);
                        Frame frame = result[0];
                        var address = Encoding.Unicode.GetString(frame.Buffer);
                        Assert.AreEqual(expectedAddress, address);
                    }
                }
                forwarderDevice.Stop();
            }
        }

I am not getting any errors but I never receive the message either 我没有收到任何错误,但我也从未收到此消息

Just looking at the latter bit of code, a classic SUB error is not to actually subscribe to anything - you need to setsockopt to SUBSCRIBE to "" (empty string) to get every message. 仅查看代码的后半部分,经典的SUB错误是实际上并未订阅任何内容-您需要将ockopt的SUBSCRIBE设置为“”(空字符串)以获取所有消息。 So, if that is a PUB/SUB socket pair, unless you're subscribing in the helper it will just be filtering the messages. 因此,如果这是一个PUB / SUB套接字对,除非您正在订阅帮助程序,否则它将仅过滤消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM