2014-03-19 49 views
0

我有一個ZMQ發行商和用戶代碼nodejs.My問題是,有時我的用戶不會收到任何messages.Below是我的代碼ZMQ模塊的NodeJS推輓工作不properly.Subscriber沒有收到消息正確

出版商:

var zmq = require('zmq'), 

socket = zmq.socket('push'), 
socket.bind(publisherUrl, function (err) { 
if (err) { 
    console.log(err); 
} 
else { 
    console.log("Listening on ..." + publisherUrl) 
} 

});

function PublishEvent(msg) { 
setTimeout(function() { 
    socket.send(msg); 
}, 100); 

} 

用戶:

var zmq = require('zmq'), 
events = require('events'), 
subscriber = zmq.socket('pull'); 

subscriber.on("message", function (reply) { 
if (reply.toString() !== '{}'){ 
console.log("info", 'Received message:' + reply.toString()); 
} 
}) 
subscriber.connect(publisherUrl) 

其工作有時正常,但未能在某些情況下,接收消息。我困在這裏對此有任何幫助都會有很大的幫助。

回答

0

您的出版商應該是這樣的:

var zmq = require('zmq') 
var publisher = zmq.socket('pub') 

publisher.bind('tcp://*:8688', function(err) { 
    if(err) 
     console.log(err) 
    else 
     console.log("Listening on 8688...") 
}) 

for (var i=1 ; i<10 ; i++) 
    setTimeout(function() { 
     console.log('sent'); 
     publisher.send("Hello there!") 
}, 1000 * i) 

process.on('SIGINT', function() { 
    publisher.close() 
    console.log('\nClosed') 
}) 

和您的用戶應該是這樣的:

var zmq = require('zmq') 
var subscriber = zmq.socket('sub') 

subscriber.on("message", function(reply) { 
    console.log('Received message: ', reply.toString()); 
}) 

subscriber.connect("tcp://localhost:8688") 
subscriber.subscribe("") 

process.on('SIGINT', function() { 
    subscriber.close() 
    console.log('\nClosed') 
}) 

來源:https://github.com/imatix/zguide/tree/master/examples/Node.js

相關問題