diff --git a/lib/client.js b/lib/client.js index 8f2ceae..b7428b7 100644 --- a/lib/client.js +++ b/lib/client.js @@ -46,7 +46,12 @@ function Client(options) { this._timeout = options.timeout || DEFAULT_TIMEOUT; this._socket = socket.client(heartbeat); - util.eventProxy(this._socket, this, "error"); + this._socket.setTimeout(this._timeout) + + util.eventProxy(this._socket, this, "error"); + util.eventProxy(this._socket, this, "connect"); + util.eventProxy(this._socket, this, "disconnect"); + util.eventProxy(this._socket, this, "close"); } nodeUtil.inherits(Client, events.EventEmitter); diff --git a/lib/socket.js b/lib/socket.js index 5d05564..d501e11 100644 --- a/lib/socket.js +++ b/lib/socket.js @@ -83,6 +83,29 @@ Socket.prototype.bind = function(endpoint) { //endpoint : String // The ZeroMQ endpoint Socket.prototype.connect = function(endpoint) { + var self = this + this._zmqSocket.on('connect', function () { + this.connected = true + self.emit('connect') + }) + + this._zmqSocket.on('disconnect', function () { + this.connected = false + self.emit('disconnect') + }) + + this._zmqSocket.on('close', function () { + this.connected = false + self.emit('close') + }) + this._zmqSocket.monitor(500, 0) + setTimeout(function() { + self._zmqSocket.unmonitor(); + if(!self.connected) { + self.emit('disconnect') + } + }, (this.timeout || DEFAULT_TIMEOUT) * 1000); //30 seconds to connect + this._zmqSocket.connect(endpoint); } @@ -146,6 +169,7 @@ MultiplexingSocket.prototype.closed = function() { }; MultiplexingSocket.prototype.setTimeout = function(timeout) { + this.timeout = timeout }; //Creates a new multiplexing socket server