delegate event broadcasting

This commit is contained in:
Ivan Socolsky 2015-03-23 12:50:00 -03:00
parent 665e0f8731
commit deaca91a3d
3 changed files with 37 additions and 9 deletions

13
app.js
View File

@ -8,6 +8,17 @@ var port = process.env.BWS_PORT || 3001;
var app = ExpressApp.start({
basePath: basePath,
});
app.listen(port);
//app.listen(port);
var server = require('http').Server(app);
var io = require('socket.io')(server);
server.listen(port);
io.sockets.on('connection', function(socket) {
socket.emit('message', {
'message': 'hello world'
});
});
console.log('Bitcore Wallet Service running on port ' + port);

View File

@ -28,6 +28,23 @@ var Notification = require('./model/notification');
var initialized = false;
var storage, blockExplorer;
function EventBroadcaster() {};
nodeutil.inherits(EventBroadcaster, events.EventEmitter);
EventBroadcaster.prototype.broadcast = function(service, args) {
this.emit(service, args);
};
var _eventBroadcasterInstance;
EventBroadcaster.singleton = function() {
if (!_eventBroadcasterInstance) {
_eventBroadcasterInstance = new EventBroadcaster();
}
return _eventBroadcasterInstance;
};
/**
* Creates an instance of the Bitcore Wallet Service.
* @constructor
@ -41,9 +58,6 @@ function WalletService() {
this.notifyTicker = 0;
};
nodeutil.inherits(WalletService, events.EventEmitter);
/**
* Initializes global settings for all instances.
* @param {Object} opts
@ -161,6 +175,9 @@ WalletService.prototype._verifySignature = function(text, signature, pubKey) {
return WalletUtils.verifyMessage(text, signature, pubKey);
};
WalletService.prototype._emit = function(args) {
EventBroadcaster.singleton().broadcast(this, args);
};
/**
* _notify
@ -182,7 +199,7 @@ WalletService.prototype._notify = function(type, data) {
ticker: this.notifyTicker++,
});
this.storage.storeNotification(walletId, n, function() {
self.emit(n);
self._emit(n);
});
};

View File

@ -1945,7 +1945,7 @@ describe('Copay server', function() {
server.getPendingTxs({}, function(err, txs) {
var tx = txs[2];
var signatures = helpers.clientSign(tx, TestData.copayers[0].xPrivKey);
sinon.spy(server, 'emit');
sinon.spy(server, '_emit');
server.signTx({
txProposalId: tx.id,
signatures: signatures,
@ -1964,9 +1964,9 @@ describe('Copay server', function() {
var types = _.pluck(notifications, 'type');
types.should.deep.equal(['NewOutgoingTx', 'TxProposalFinallyAccepted', 'TxProposalAcceptedBy']);
// Check also events
server.emit.getCall(0).args[0].type.should.equal('TxProposalAcceptedBy');
server.emit.getCall(1).args[0].type.should.equal('TxProposalFinallyAccepted');;
server.emit.getCall(2).args[0].type.should.equal('NewOutgoingTx');
server._emit.getCall(0).args[0].type.should.equal('TxProposalAcceptedBy');
server._emit.getCall(1).args[0].type.should.equal('TxProposalFinallyAccepted');;
server._emit.getCall(2).args[0].type.should.equal('NewOutgoingTx');
done();
});