我尝试了各种不同的东西,但这基本上就是我想要做的。正在进行pubsub设置。我重新编写了这段代码,我试图将pub套接字连接到转发器设备的前端时拒绝连接。
这是代码
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首先我知道我做错了......在您尝试连接设备之前,应该在创建设备之后直接启动。第二件事是设备是在另一个线程上启动的,并且在主线程尝试连接时可能还没有准备好,所以你需要阻止一些如何准备就绪。 代码现在看起来像:
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();
}
}
我没有收到任何错误,但我从未收到任何消息
答案 0 :(得分:0)
只看后面的代码,经典的SUB错误不是实际订阅任何东西 - 你需要setsockopt到SUBSCRIBE到“”(空字符串)来获取每条消息。所以,如果那是一个PUB / SUB套接字对,除非你订阅了帮助器,否则它只是过滤消息。