2014-01-13 13:13:41 -08:00
|
|
|
'use strict';
|
|
|
|
|
2014-01-14 10:20:59 -08:00
|
|
|
// server-side socket behaviour
|
2014-01-16 18:23:10 -08:00
|
|
|
// io is a variable already taken in express
|
|
|
|
var ios = null;
|
2014-02-12 06:59:29 -08:00
|
|
|
var util = require('bitcore/util/util');
|
2014-01-14 11:56:02 -08:00
|
|
|
|
|
|
|
module.exports.init = function(app, io_ext) {
|
2014-01-16 18:23:10 -08:00
|
|
|
ios = io_ext;
|
|
|
|
ios.set('log level', 1); // reduce logging
|
2014-01-17 10:25:15 -08:00
|
|
|
ios.sockets.on('connection', function(socket) {
|
|
|
|
socket.on('subscribe', function(topic) {
|
|
|
|
socket.join(topic);
|
|
|
|
});
|
2014-01-13 13:13:41 -08:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2014-02-09 14:33:39 -08:00
|
|
|
module.exports.broadcastTx = function(tx) {
|
2014-02-10 14:11:13 -08:00
|
|
|
if (ios) {
|
2014-02-18 09:51:45 -08:00
|
|
|
var t;
|
2014-02-10 14:11:13 -08:00
|
|
|
if (typeof tx === 'string') {
|
2014-02-13 11:24:17 -08:00
|
|
|
t = {
|
|
|
|
txid: tx
|
|
|
|
};
|
2014-02-18 09:51:45 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
else {
|
|
|
|
t = {
|
|
|
|
txid: tx.txid,
|
|
|
|
size: tx.size,
|
|
|
|
};
|
2014-02-10 14:11:13 -08:00
|
|
|
// Outputs
|
|
|
|
var valueOut = 0;
|
2014-02-18 09:51:45 -08:00
|
|
|
tx.vout.forEach(function(o) {
|
2014-02-12 06:59:29 -08:00
|
|
|
valueOut += o.value * util.COIN;
|
2014-02-10 14:11:13 -08:00
|
|
|
});
|
|
|
|
|
2014-02-12 06:59:29 -08:00
|
|
|
t.valueOut = parseInt(valueOut) / util.COIN;
|
2014-02-10 14:11:13 -08:00
|
|
|
}
|
2014-02-13 11:24:17 -08:00
|
|
|
ios.sockets. in ('inv').emit('tx', t);
|
2014-02-10 14:11:13 -08:00
|
|
|
}
|
2014-01-14 11:56:02 -08:00
|
|
|
};
|
2014-01-14 14:42:38 -08:00
|
|
|
|
2014-02-09 14:33:39 -08:00
|
|
|
module.exports.broadcastBlock = function(block) {
|
2014-02-13 11:24:17 -08:00
|
|
|
if (ios) ios.sockets. in ('inv').emit('block', block);
|
2014-01-14 14:42:38 -08:00
|
|
|
};
|
2014-01-17 10:25:15 -08:00
|
|
|
|
2014-02-09 14:33:39 -08:00
|
|
|
module.exports.broadcastAddressTx = function(address, tx) {
|
2014-02-13 11:24:17 -08:00
|
|
|
if (ios) ios.sockets. in (address).emit(address, tx);
|
2014-01-17 10:25:15 -08:00
|
|
|
};
|
|
|
|
|
2014-01-21 14:13:21 -08:00
|
|
|
module.exports.broadcastSyncInfo = function(historicSync) {
|
2014-02-12 14:27:29 -08:00
|
|
|
|
2014-02-13 11:24:17 -08:00
|
|
|
if (ios) {
|
|
|
|
ios.sockets. in ('sync').emit('status', historicSync);
|
|
|
|
}
|
2014-01-19 07:33:39 -08:00
|
|
|
};
|