在初始化我的路由器应用程序时,我调用以下代码。它绑定得很好,接收消息很好,但拒绝为On_ReceiveXXX方法工作,除非它是直接响应。我想知道为什么
public void Initialize(string frontEndAddress, string backEndAddress)
{
_poller = new Poller();
_timeAllowedBetweenPings = TimeSpan.FromMinutes(1);
_lastPingResponse = DateTime.Now;
using (var ctx = NetMQContext.Create())
{
_frontEnd = ctx.CreateRouterSocket();
_backEnd = ctx.CreateRouterSocket();
_frontEnd.Bind(frontEndAddress);
Console.WriteLine(string.Format("[Router]: Connected to {0}", frontEndAddress));
_backEnd.Bind(backEndAddress);
Console.WriteLine(string.Format("[Router]: Connected to {0}", backEndAddress));
_frontEnd.ReceiveReady += On_ReceiveFrontEnd;
_backEnd.ReceiveReady += On_ReceiveBackEnd;
_poller.AddSocket(_frontEnd);
_poller.AddSocket(_backEnd);
var timer = new NetMQTimer(TimeSpan.FromSeconds(1));
timer.Elapsed += On_Ping;
_poller.AddTimer(timer);
_poller.PollTillCancelled();
}
}
这无法致电经销商ReceiveReady事件:
private void On_ReceiveFrontEnd(object sender,NetMQSocketEventArgs e) { _lastPingResponse = DateTime.Now; var frontEndMsg = e.Socket.ReceiveMultipartBytes(); var streamData = frontEndMsg.Last(); ApplicationMessage msg = PackageHelper.DeserializeOutgoing(streamData); Console.WriteLine(string.Format(“收到的命令:{0}”,msg.CO));
_backEnd.SendMultipartBytes(frontEndMsg);
}
但如果我改变了行
_backEnd.SendMultipartBytes(frontEndMsg);
到
_frontEnd.SendMultipartBytes(frontEndMsg);
它突然起作用......所以来自我的前端应用程序的消息只能被响应,而不会传递给后端应用程序。对于后端消息,反过来也是如此。
答案 0 :(得分:0)
使用路由器时,第一帧是路由ID,它是特定于套接字的。因此,您无法将整个消息从路由器传递到路由器。将后端更改为经销商,它将起作用,或者在消息前面加上后端套接字的路由ID。