-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathshovellisteners.js
42 lines (37 loc) · 1.17 KB
/
shovellisteners.js
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
//eventsource for listening to bitsocket streams
const utils = require('./utils')
const EventSource = require('eventsource');
const BITSOCKET_SOURCE = 'https://bitgraph.network/s/'
//maintains list of active bitsocket listeners
let BITSOCKET_LISTENERS = [];
let listen = function listen (name, query) {
const listener = {
name : name,
bitsocket : createEventSource(query)
}
BITSOCKET_LISTENERS.push(listener);
return listener
}
function createEventSource(query) {
console.log(query)
const url = utils.urlString(BITSOCKET_SOURCE, query)
return new EventSource(url)
}
//stop listening to bitsocket messages
//this will shut down all bitcoin tx from broadcasting on your local bus
//you can still send tx (using CHANNEL_SEND)
let close = function close(name) {
let soxlen = BITSOCKET_LISTENERS.length;
while (soxlen--) {
const listener = BITSOCKET_LISTENERS[soxlen];
if (listener.name === name) {
console.log(`stopping bitsocket listener ${listener.name}`)
listener.bitsocket.close();
BITSOCKET_LISTENERS.splice(soxlen, 1);
}
}
}
module.exports = {
listen: listen,
close: close
}