diff --git a/lib/bus.js b/lib/bus.js index 720261d0..d4f3bdd5 100644 --- a/lib/bus.js +++ b/lib/bus.js @@ -13,6 +13,7 @@ var util = require('util'); function Bus(params) { events.EventEmitter.call(this); this.node = params.node; + this.remoteAddress = params.remoteAddress; } util.inherits(Bus, events.EventEmitter); diff --git a/lib/node.js b/lib/node.js index 75431598..c2350b50 100644 --- a/lib/node.js +++ b/lib/node.js @@ -87,8 +87,11 @@ Node.prototype._setNetwork = function(config) { * Will instantiate a new Bus for this node. * @returns {Bus} */ -Node.prototype.openBus = function() { - return new Bus({node: this}); +Node.prototype.openBus = function(options) { + if (!options) { + options = {}; + } + return new Bus({node: this, remoteAddress: options.remoteAddress}); }; /** diff --git a/lib/services/bitcoind.js b/lib/services/bitcoind.js index 632b7a12..04055a47 100644 --- a/lib/services/bitcoind.js +++ b/lib/services/bitcoind.js @@ -195,6 +195,7 @@ Bitcoin.prototype.getPublishEvents = function() { Bitcoin.prototype.subscribe = function(name, emitter) { this.subscriptions[name].push(emitter); + log.info(emitter.remoteAddress, 'subscribing:', 'bitcoind/' + name, 'total:', this.subscriptions[name].length); }; Bitcoin.prototype.unsubscribe = function(name, emitter) { @@ -202,6 +203,7 @@ Bitcoin.prototype.unsubscribe = function(name, emitter) { if (index > -1) { this.subscriptions[name].splice(index, 1); } + log.info(emitter.remoteAddress, 'unsubscribing:', 'bitcoind/' + name, 'total:', this.subscriptions[name].length); }; Bitcoin.prototype._getDefaultConfig = function() { diff --git a/lib/services/web.js b/lib/services/web.js index 1e292676..0008b745 100644 --- a/lib/services/web.js +++ b/lib/services/web.js @@ -158,23 +158,30 @@ WebService.prototype.getEventNames = function() { return eventNames; }; +WebService.prototype._getRemoteAddress = function(socket) { + return socket.conn.remoteAddress; +}; + /** * This function is responsible for managing a socket.io connection, including * instantiating a new Bus, subscribing/unsubscribing and handling RPC commands. * @param {Socket} socket - A socket.io socket instance */ WebService.prototype.socketHandler = function(socket) { - var bus = this.node.openBus(); + var self = this; + var bus = this.node.openBus({remoteAddress: self._getRemoteAddress(socket)}); if (this.enableSocketRPC) { socket.on('message', this.socketMessageHandler.bind(this)); } socket.on('subscribe', function(name, params) { + log.info(self._getRemoteAddress(socket), 'web socket subscribe:', name); bus.subscribe(name, params); }); socket.on('unsubscribe', function(name, params) { + log.info(self._getRemoteAddress(socket), 'web socket unsubscribe:', name); bus.unsubscribe(name, params); }); @@ -194,6 +201,7 @@ WebService.prototype.socketHandler = function(socket) { }); socket.on('disconnect', function() { + log.info(self._getRemoteAddress(socket), 'web socket disconnect'); bus.close(); }); }; diff --git a/test/services/web.unit.js b/test/services/web.unit.js index 5a582784..c0f720d6 100644 --- a/test/services/web.unit.js +++ b/test/services/web.unit.js @@ -212,6 +212,7 @@ describe('WebService', function() { describe('#socketHandler', function() { var bus = new EventEmitter(); + bus.remoteAddress = '127.0.0.1'; var Module1 = function() {}; Module1.prototype.getPublishEvents = function() { @@ -241,6 +242,8 @@ describe('WebService', function() { done(); }; socket = new EventEmitter(); + socket.conn = {}; + socket.conn.remoteAddress = '127.0.0.1'; web.socketHandler(socket); socket.emit('message', 'data'); }); @@ -250,6 +253,8 @@ describe('WebService', function() { web.eventNames = web.getEventNames(); web.socketMessageHandler = sinon.stub(); socket = new EventEmitter(); + socket.conn = {}; + socket.conn.remoteAddress = '127.0.0.1'; web.socketHandler(socket); socket.on('message', function() { web.socketMessageHandler.callCount.should.equal(0);