web: added logging for web socket events
This commit is contained in:
parent
fa6474e85f
commit
a48bcaf900
|
@ -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);
|
||||
|
|
|
@ -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});
|
||||
};
|
||||
|
||||
/**
|
||||
|
|
|
@ -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() {
|
||||
|
|
|
@ -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();
|
||||
});
|
||||
};
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in New Issue