bitcore-node-zcash/app/controllers/socket.js

58 lines
1.2 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-05-07 06:42:45 -07:00
var util = require('bitcore').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) {
2014-02-18 09:51:45 -08:00
var t;
2014-02-10 14:11:13 -08:00
if (typeof tx === 'string') {
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-05-25 19:54:08 -07:00
valueOut += o.valueSat;
2014-02-10 14:11:13 -08:00
});
t.valueOut = (valueOut.toFixed(8)/util.COIN);
2014-02-10 14:11:13 -08:00
}
2014-03-20 10:06:37 -07: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-03-20 10:06:37 -07: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-03-20 10:06:37 -07:00
if (ios)
ios.sockets.in(address).emit(address, tx);
};
2014-01-21 14:13:21 -08:00
module.exports.broadcastSyncInfo = function(historicSync) {
2014-03-20 10:06:37 -07:00
if (ios)
ios.sockets.in('sync').emit('status', historicSync);
};