嗨,我使用下面的 zeromq 在 nodejs 中编写发布者和订阅者是我的代码
发布者.js
var zmq = require('zmq')
var publisher = zmq.socket('pub')
publisher.bind('tcp://127.0.0.1:7000', function(err) {
if(err)
console.log(err)
else
console.log("Listening on 7000...")
})
setTimeout(function() {
console.log('sent');
publisher.send("hi")
}, 1000)
process.on('SIGINT', function() {
publisher.close()
console.log('\nClosed')
})
订阅者.js
var zmq = require('zmq')
var subscriber = zmq.socket('sub')
subscriber.on("message", function(reply) {
console.log('Received message: ', reply.toString());
})
subscriber.connect("tcp://localhost:7000",function(err) {
if (err) {
console.log( 'Error binding socket' );
return;
}
subscriber.close(); // This is fine! The socket is ready!
})
subscriber.subscribe("")
process.on('SIGINT', function() {
subscriber.close()
console.log('\nClosed')
})
当发布者关闭或订阅者关闭时,我试图通过写回电话来捕获错误。但在这两种情况下,似乎都没有捕获 srror。我被困在这里不知道哪里出了问题。任何帮助都会非常感激。