insight-ui-zcash/app/controllers/socket.js

52 lines
1.1 KiB
JavaScript
Raw Normal View History

2014-01-13 13:13:41 -08:00
'use strict';
// server-side socket behaviour
// 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) {
ios = io_ext;
ios.set('log level', 1); // reduce logging
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) {
var t = {};
if (typeof tx === 'string') {
t = { txid: tx };
}
else {
t = tx;
// Outputs
var valueOut = 0;
t.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
}
ios.sockets.in('inv').emit('tx', t);
}
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-01-23 10:10:52 -08:00
if (ios) ios.sockets.in('inv').emit('block', block);
2014-01-14 14:42:38 -08:00
};
2014-02-09 14:33:39 -08:00
module.exports.broadcastAddressTx = function(address, tx) {
2014-01-23 10:10:52 -08:00
if (ios) ios.sockets.in(address).emit(address, tx);
};
2014-01-21 14:13:21 -08:00
module.exports.broadcastSyncInfo = function(historicSync) {
if (ios) {
ios.sockets.in('sync').emit('status', historicSync);
}
};