new transactions shown on homepage!

This commit is contained in:
Manuel Araoz 2014-01-14 16:56:02 -03:00
parent 851dc06a09
commit cc7c876247
5 changed files with 22 additions and 8 deletions

View File

@ -91,18 +91,20 @@ TransactionSchema.statics.fromIdWithInfo = function(txid, cb) {
TransactionSchema.statics.createFromArray = function(txs, next) { TransactionSchema.statics.createFromArray = function(txs, next) {
var that = this; var that = this;
if (!txs) return next(); if (!txs) return next();
var mongo_txs = [];
async.forEach( txs, async.forEach( txs,
function(tx, callback) { function(tx, callback) {
that.create({ txid: tx }, function(err) { that.create({ txid: tx }, function(err, new_tx) {
if (err && ! err.toString().match(/E11000/)) { if (err && ! err.toString().match(/E11000/)) {
return callback(err); return callback(err);
} }
console.log(new_tx);
mongo_txs.push(new_tx);
return callback(); return callback();
}); });
}, },
function(err) { function(err) {
return next(err); return next(err, mongo_txs);
} }
); );
}; };

View File

@ -3,7 +3,11 @@
var Transaction = require('../../models/Transaction'); var Transaction = require('../../models/Transaction');
// server-side socket behaviour // server-side socket behaviour
module.exports = function(app, io) {
var io = null;
module.exports.init = function(app, io_ext) {
io = io_ext;
io.set('log level', 1); // reduce logging io.set('log level', 1); // reduce logging
io.sockets.on('connection', function(socket) { io.sockets.on('connection', function(socket) {
Transaction.findOne(function(err, tx) { Transaction.findOne(function(err, tx) {
@ -14,3 +18,7 @@ module.exports = function(app, io) {
}); });
}; };
module.exports.broadcast_tx = function(tx) {
io.sockets.emit('tx', tx);
};

View File

@ -13,6 +13,7 @@ function spec() {
var Block = require('../app/models/Block'); var Block = require('../app/models/Block');
var Transaction = require('../app/models/Transaction'); var Transaction = require('../app/models/Transaction');
var TransactionItem = require('../app/models/TransactionItem'); var TransactionItem = require('../app/models/TransactionItem');
var sockets = require('../app/views/sockets/main.js');
function Sync(config) { function Sync(config) {
this.tx_count =0; this.tx_count =0;
@ -61,11 +62,13 @@ function spec() {
Sync.prototype.storeTxs = function(txids, cb) { Sync.prototype.storeTxs = function(txids, cb) {
var that=this; var that=this;
Transaction.createFromArray(txids, function(err) { Transaction.createFromArray(txids, function(err, inserted_txs) {
if (err) return cb(err); if (err) return cb(err);
async.each(txids, function(txid, next) { async.each(inserted_txs, function(new_tx, next) {
var txid = new_tx.txid;
sockets.broadcast_tx(new_tx);
// This will trigger an RPC call // This will trigger an RPC call
Transaction.explodeTransactionItems( txid, function(err) { Transaction.explodeTransactionItems( txid, function(err) {
that.tx_count++; that.tx_count++;
@ -202,6 +205,7 @@ function spec() {
if (!(opts && opts.skip_db_connection)) { if (!(opts && opts.skip_db_connection)) {
mongoose.connect(config.db); mongoose.connect(config.db);
} }
this.opts = opts;
this.db = mongoose.connection; this.db = mongoose.connection;
this.rpc = new RpcClient(config.bitcoind); this.rpc = new RpcClient(config.bitcoind);

View File

@ -5,7 +5,7 @@ angular.module('mystery.system').controller('IndexController', ['$scope', 'Globa
socket.on('tx', function(data) { socket.on('tx', function(data) {
var tx = data; var tx = data;
console.log('Transaction received! ' + tx.txid); console.log('Transaction received! ' + tx.txid);
$scope.txs.push(tx.txid); $scope.txs.unshift(tx.txid);
}); });
$scope.txs = []; $scope.txs = [];

View File

@ -59,7 +59,7 @@ require('./config/routes')(app);
// socket.io // socket.io
var server = require('http').createServer(app); var server = require('http').createServer(app);
var io = require('socket.io').listen(server); var io = require('socket.io').listen(server);
require('./app/views/sockets/main.js')(app,io); require('./app/views/sockets/main.js').init(app,io);
//Start the app by listening on <port> //Start the app by listening on <port>
var port = process.env.PORT || config.port; var port = process.env.PORT || config.port;