zeromq REP节点将只获得第一个消息(Req,Rep模式)

我有一个nodejs应用程序,它侦听来自客户端(python应用程序)的消息。 我通过zmq进行通信的模式是REQ / REP模式。 主应用程序应该从许多客户端收到消息。 它不会回复他们,只是得到消息。 问题是主应用程序只会获得第一条消息,而下一条消息不会显示在nodejs应用程序控制台中。

换句话说,每次我启动nodejs应用程序,我只能得到一个消息。

这里是我的代码:

Nodejs应用程序

var responder = zmq.socket('rep'); responder.on('message', function(request) { console.log(request); //here, it seems this function will be called just once! }); responder.bind('tcp://127.0.0.1:8000', function(err) { if (err) { console.log(err); } else { console.log('Listening on 8000...'); } }); 

python(客户端)部分:

 socket = context.socket(zmq.REQ) socket.connect("tcp://127.0.0.1:8000") socket.send('blaaaa') print 'message sent!' 

python部分是在一个函数里面的。 我可以看到“发送的消息!”的输出 在python控制台(我的意思是很多'消息发送!')。 但是我看不到nodejs app.just中的消息,第一条消息在nodejs的控制台中可见。

在使用REQ / REP模式时,您实际上需要在给出下一个请求之前对请求做出响应 – 您将只处理一个请求。

 var responder = zmq.socket('rep'); responder.on('message', function(request) { console.log(request); responder.send('Here comes the reply!'); }); 

回应,你会收到下一个。 如果你不想回应,那么你需要select比req / rep-ex更好的其他套接字:push / pull,或者如果你想同时处理多个请求,可以看看xreq / xrep(路由器/经销商) 。

如果有疑问,请在http://api.zeromq.org/2-1:zmq-socket查找每个套接字types的发送/接收模式