From deaca91a3d0f3de66740bfde534ff21b47c565aa Mon Sep 17 00:00:00 2001 From: Ivan Socolsky Date: Mon, 23 Mar 2015 12:50:00 -0300 Subject: [PATCH] delegate event broadcasting --- app.js | 13 ++++++++++++- lib/server.js | 25 +++++++++++++++++++++---- test/integration/server.js | 8 ++++---- 3 files changed, 37 insertions(+), 9 deletions(-) diff --git a/app.js b/app.js index 1a0ee5f..a8ea379 100644 --- a/app.js +++ b/app.js @@ -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); diff --git a/lib/server.js b/lib/server.js index 8864375..5f26fc9 100644 --- a/lib/server.js +++ b/lib/server.js @@ -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); }); }; diff --git a/test/integration/server.js b/test/integration/server.js index f726385..877f0dd 100644 --- a/test/integration/server.js +++ b/test/integration/server.js @@ -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(); });