我正在尝试构建Paranoid Pirate Pattern的后半部分,这是一个将工作发送到一组经销商节点的路由器(我可能会误解图表) )。现在我只想让经销商回复工作,或者只是发回一条消息,说明"已完成"。问题是工作节点(DEALER)从不接收任何消息。
var buildSocket, connectionTemplate, delay, frontPort, log, q, qPort, worker, workerPort, zmq;
zmq = require("zmq");
frontPort = 5000;
qPort = 5100;
workerPort = 5200;
connectionTemplate = "tcp://127.0.0.1:";
log = console.log;
debugger;
delay = process.argv[2] || 1000;
buildSocket = function(desc, socketType, port) {
var socket;
log("creating " + socketType + " socket");
socket = zmq.socket(socketType);
socket.identity = "" + desc + "-" + socketType + "-" + process.pid + "-" + port;
return socket;
};
q = buildSocket('q_output', 'router', qPort);
worker = buildSocket('worker', 'dealer', workerPort);
worker.bind("" + connectionTemplate + workerPort);
q.connect("" + connectionTemplate + workerPort);
q.on('message', function() {
var args;
args = Array.apply(null, arguments);
log('queue received ' + JSON.stringify(arguments));
return worker.send('work done');
});
worker.on('message', function() {
var args;
log('back received ' + JSON.stringify(arguments));
args = Array.apply(null, arguments);
return q.send(args);
});
setInterval((function() {
var value;
value = Math.floor(Math.random() * 100);
console.log(q.identity + ": sending " + value);
q.send(value);
}), delay);
队列和工作人员on 'message'
事件永远不会触发。我理解这一点的方法是设置ROUTER节点,将其绑定到端口(用于返回消息),设置DEALER节点并将它们绑定到端口,然后将ROUTER连接到DEALER端口并开始发送消息。实际上,消息是发送但从未收到过:
creating router socket
creating dealer socket
q_output-router-60326-5100: sending 30
q_output-router-60326-5100: sending 25
q_output-router-60326-5100: sending 65
q_output-router-60326-5100: sending 68
q_output-router-60326-5100: sending 50
q_output-router-60326-5100: sending 88
答案 0 :(得分:3)
在这里,你有点倒退了。将DEALER
套接字视为已修改的REQ
套接字...它应该将您的消息发送到路由器。 ROUTER
套接字更像是修改后的REP
套接字......它应该响应您的经销商发送的初始请求。
你 在你学习的过程中坚持下去。
从您的代码中向我发出的第二件事就是您发送消息处理程序,您发送了错误的套接字消息。
以此代码为例(直接复制而不做修改):
ROUTER/DEALER
...说(在伪代码中):
q.on('message', function() {
var args;
args = Array.apply(null, arguments);
log('queue received ' + JSON.stringify(arguments));
return worker.send('work done');
});
你想要的更像是以下(简化):
when `q` receives a message from `worker`
print out the message we received
now have `worker` send *another* message that says "work done"
......如你所见,模式是: