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) {
|
function Bus(params) {
|
||||||
events.EventEmitter.call(this);
|
events.EventEmitter.call(this);
|
||||||
this.node = params.node;
|
this.node = params.node;
|
||||||
|
this.remoteAddress = params.remoteAddress;
|
||||||
}
|
}
|
||||||
|
|
||||||
util.inherits(Bus, events.EventEmitter);
|
util.inherits(Bus, events.EventEmitter);
|
||||||
|
|||||||
@ -87,8 +87,11 @@ Node.prototype._setNetwork = function(config) {
|
|||||||
* Will instantiate a new Bus for this node.
|
* Will instantiate a new Bus for this node.
|
||||||
* @returns {Bus}
|
* @returns {Bus}
|
||||||
*/
|
*/
|
||||||
Node.prototype.openBus = function() {
|
Node.prototype.openBus = function(options) {
|
||||||
return new Bus({node: this});
|
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) {
|
Bitcoin.prototype.subscribe = function(name, emitter) {
|
||||||
this.subscriptions[name].push(emitter);
|
this.subscriptions[name].push(emitter);
|
||||||
|
log.info(emitter.remoteAddress, 'subscribing:', 'bitcoind/' + name, 'total:', this.subscriptions[name].length);
|
||||||
};
|
};
|
||||||
|
|
||||||
Bitcoin.prototype.unsubscribe = function(name, emitter) {
|
Bitcoin.prototype.unsubscribe = function(name, emitter) {
|
||||||
@ -202,6 +203,7 @@ Bitcoin.prototype.unsubscribe = function(name, emitter) {
|
|||||||
if (index > -1) {
|
if (index > -1) {
|
||||||
this.subscriptions[name].splice(index, 1);
|
this.subscriptions[name].splice(index, 1);
|
||||||
}
|
}
|
||||||
|
log.info(emitter.remoteAddress, 'unsubscribing:', 'bitcoind/' + name, 'total:', this.subscriptions[name].length);
|
||||||
};
|
};
|
||||||
|
|
||||||
Bitcoin.prototype._getDefaultConfig = function() {
|
Bitcoin.prototype._getDefaultConfig = function() {
|
||||||
|
|||||||
@ -158,23 +158,30 @@ WebService.prototype.getEventNames = function() {
|
|||||||
return eventNames;
|
return eventNames;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
WebService.prototype._getRemoteAddress = function(socket) {
|
||||||
|
return socket.conn.remoteAddress;
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This function is responsible for managing a socket.io connection, including
|
* This function is responsible for managing a socket.io connection, including
|
||||||
* instantiating a new Bus, subscribing/unsubscribing and handling RPC commands.
|
* instantiating a new Bus, subscribing/unsubscribing and handling RPC commands.
|
||||||
* @param {Socket} socket - A socket.io socket instance
|
* @param {Socket} socket - A socket.io socket instance
|
||||||
*/
|
*/
|
||||||
WebService.prototype.socketHandler = function(socket) {
|
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) {
|
if (this.enableSocketRPC) {
|
||||||
socket.on('message', this.socketMessageHandler.bind(this));
|
socket.on('message', this.socketMessageHandler.bind(this));
|
||||||
}
|
}
|
||||||
|
|
||||||
socket.on('subscribe', function(name, params) {
|
socket.on('subscribe', function(name, params) {
|
||||||
|
log.info(self._getRemoteAddress(socket), 'web socket subscribe:', name);
|
||||||
bus.subscribe(name, params);
|
bus.subscribe(name, params);
|
||||||
});
|
});
|
||||||
|
|
||||||
socket.on('unsubscribe', function(name, params) {
|
socket.on('unsubscribe', function(name, params) {
|
||||||
|
log.info(self._getRemoteAddress(socket), 'web socket unsubscribe:', name);
|
||||||
bus.unsubscribe(name, params);
|
bus.unsubscribe(name, params);
|
||||||
});
|
});
|
||||||
|
|
||||||
@ -194,6 +201,7 @@ WebService.prototype.socketHandler = function(socket) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
socket.on('disconnect', function() {
|
socket.on('disconnect', function() {
|
||||||
|
log.info(self._getRemoteAddress(socket), 'web socket disconnect');
|
||||||
bus.close();
|
bus.close();
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|||||||
@ -212,6 +212,7 @@ describe('WebService', function() {
|
|||||||
|
|
||||||
describe('#socketHandler', function() {
|
describe('#socketHandler', function() {
|
||||||
var bus = new EventEmitter();
|
var bus = new EventEmitter();
|
||||||
|
bus.remoteAddress = '127.0.0.1';
|
||||||
|
|
||||||
var Module1 = function() {};
|
var Module1 = function() {};
|
||||||
Module1.prototype.getPublishEvents = function() {
|
Module1.prototype.getPublishEvents = function() {
|
||||||
@ -241,6 +242,8 @@ describe('WebService', function() {
|
|||||||
done();
|
done();
|
||||||
};
|
};
|
||||||
socket = new EventEmitter();
|
socket = new EventEmitter();
|
||||||
|
socket.conn = {};
|
||||||
|
socket.conn.remoteAddress = '127.0.0.1';
|
||||||
web.socketHandler(socket);
|
web.socketHandler(socket);
|
||||||
socket.emit('message', 'data');
|
socket.emit('message', 'data');
|
||||||
});
|
});
|
||||||
@ -250,6 +253,8 @@ describe('WebService', function() {
|
|||||||
web.eventNames = web.getEventNames();
|
web.eventNames = web.getEventNames();
|
||||||
web.socketMessageHandler = sinon.stub();
|
web.socketMessageHandler = sinon.stub();
|
||||||
socket = new EventEmitter();
|
socket = new EventEmitter();
|
||||||
|
socket.conn = {};
|
||||||
|
socket.conn.remoteAddress = '127.0.0.1';
|
||||||
web.socketHandler(socket);
|
web.socketHandler(socket);
|
||||||
socket.on('message', function() {
|
socket.on('message', function() {
|
||||||
web.socketMessageHandler.callCount.should.equal(0);
|
web.socketMessageHandler.callCount.should.equal(0);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user