-
Notifications
You must be signed in to change notification settings - Fork 1k
Expand file tree
/
Copy pathsocket.js
More file actions
57 lines (48 loc) · 1.2 KB
/
socket.js
File metadata and controls
57 lines (48 loc) · 1.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
'use strict';
// server-side socket behaviour
// io is a variable already taken in express
var ios = null;
var util = require('bitcore/util/util');
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);
});
});
};
module.exports.broadcastTx = function(tx) {
if (ios) {
var t;
if (typeof tx === 'string') {
t = {
txid: tx
};
}
else {
t = {
txid: tx.txid,
size: tx.size,
};
// Outputs
var valueOut = 0;
tx.vout.forEach(function(o) {
valueOut += o.value * util.COIN;
});
t.valueOut = parseInt(valueOut) / util.COIN;
}
ios.sockets. in ('inv').emit('tx', t);
}
};
module.exports.broadcastBlock = function(block) {
if (ios) ios.sockets. in ('inv').emit('block', block);
};
module.exports.broadcastAddressTx = function(address, tx) {
if (ios) ios.sockets. in (address).emit(address, tx);
};
module.exports.broadcastSyncInfo = function(historicSync) {
if (ios) {
ios.sockets. in ('sync').emit('status', historicSync);
}
};