我嘗試了各種不同的東西,但這基本上是我想要做的。正在建立一個正在處理的pubsub。我重新編寫了這段代碼,並且在嘗試將酒吧套接字連接到轉發器設備的前端時,我得到的連接被拒絕。任何人都可以告訴我這個ZeroMQ代碼有什麼問題嗎?
下面是代碼
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();
}
}
我知道我做錯了好第一件事......開始應直接創建設備來後,你嘗試連接到它。第二件事是設備在另一個線程上啓動,並且在主線程嘗試連接時可能沒有準備好,因此您需要阻止一些操作,直到它準備就緒。 代碼現在看起來像:
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();
}
}
我沒有得到任何錯誤,但我從來沒有收到該郵件或者
請包括您在您的問題中得到的錯誤 – TimothyP 2013-03-06 05:57:04