Browse Source

Updated subscribe methods in helpers/index.js.

Added subscribedHeaders to helpers/clients.js.
Updated onMessage, onClose & onError methods in lib/client.js.
get-transaction-merkle
Corey Phillips 4 years ago
parent
commit
b360cc67b7
No known key found for this signature in database GPG Key ID: 80C0975F55D3A07B
  1. 12
      helpers/clients.js
  2. 20
      helpers/index.js
  3. 37
      lib/client.js

12
helpers/clients.js

@ -17,20 +17,24 @@ class Clients {
bitcoin: [],
bitcoinTestnet: []
};
this.subscribedHeaders = {
bitcoin: false,
bitcoinTestnet: false,
};
}
updateNetwork(network) {
this.network = network;
}
updateMainClient(mainClient) {
this.mainClient = mainClient;
}
updatePeer(peer) {
this.peer = peer;
}
}
module.exports = new Clients();

20
helpers/index.js

@ -286,12 +286,12 @@ const getPeers = ({ id = Math.random(), network = "" } = {}) => {
const subscribeHeader = async ({ id = "subscribeHeader", network = "", onReceive = () => null } = {}) => {
try {
if (clients.mainClient[network] === false) await connectToRandomPeer(network, clients.peers[network]);
clients.mainClient[network].subscribe.on('blockchain.headers.subscribe', (data) => {
console.log("Received header.");
console.log(data);
onReceive(data)
});
return { id, error: false, method: "subscribeHeader", data: "Subscribed", network };
if (clients.subscribedHeaders[network] === true) return { id, error: false, method: "subscribeHeader", data: 'Already Subscribed.', network };
const res = await promiseTimeout(10000, clients.mainClient[network].subscribe.on('blockchain.headers.subscribe', (onReceive)));
if (res.error) return { ...res, id, method: "subscribeHeader" };
const response = await promiseTimeout(10000, clients.mainClient[network].blockchainHeaders_subscribe());
if (!response.error) clients.subscribedHeaders[network] = true;
return { ...response, id, method: "subscribeHeader" };
} catch (e) {
return { id, error: true, method: "subscribeHeader", data: e, network };
}
@ -300,10 +300,12 @@ const subscribeHeader = async ({ id = "subscribeHeader", network = "", onReceive
const subscribeAddress = async ({ id = Math.random(), scriptHash = "", network = "bitcoin", onReceive = (data) => console.log(data) } = {}) => {
try {
if (clients.mainClient[network] === false) await connectToRandomPeer(network, clients.peers[network]);
if (clients.subscribedAddresses[network].length < 1) {
const res = await promiseTimeout(10000, clients.mainClient[network].subscribe.on('blockchain.scripthash.subscribe', (data => onReceive(data))));
if (res.error) return { ...res, id, method: "subscribeAddress" };
}
//Ensure this address is not already subscribed
if (clients.subscribedAddresses[network].includes(scriptHash)) return { id, error: false, method: "subscribeAddress", data: "" };
const res = await promiseTimeout(10000, clients.mainClient[network].subscribe.on('blockchain.scripthash.subscribe', (onReceive)));
if (res.error) return { ...res, id, method: "subscribeAddress" };
if (clients.subscribedAddresses[network].includes(scriptHash)) return { id, error: false, method: "subscribeAddress", data: "Already Subscribed." };
const response = await promiseTimeout(10000, clients.mainClient[network].blockchainScripthash_subscribe(scriptHash));
if (!response.error) clients.subscribedAddresses[network].push(scriptHash);
return { ...response, id, method: "subscribeAddress" };

37
lib/client.js

@ -139,29 +139,21 @@ class Client {
}
onMessage(body, n) {
const msg = JSON.parse(body);
if (msg instanceof Array) {
this.response(msg);
} else {
if (msg.id !== void 0) {
try {
const msg = JSON.parse(body);
if (msg instanceof Array) {
this.response(msg);
} else {
this.subscribe.emit(msg.method, msg.params);
if (msg.id !== void 0) {
this.response(msg);
} else {
this.subscribe.emit(msg.method, msg.params);
}
}
} catch (error) {
this.conn.end();
this.conn.destroy();
this.onClose(error);
try {
const msg = JSON.parse(body);
if (msg instanceof Array) {
this.response(msg);
} else {
if (msg.id !== void 0) {
this.response(msg);
} else {
this.subscribe.emit(msg.method, msg.params);
}
}
} catch (error) {
this.conn.end();
this.conn.destroy();
this.onClose(error);
}
}
@ -169,6 +161,7 @@ class Client {
}
onClose(){
this.status = 0;
Object.keys(this.callback_message_queue).forEach((key) => {
this.callback_message_queue[key](new Error('close connect'))
delete this.callback_message_queue[key]
@ -180,7 +173,7 @@ class Client {
}
onError(e){
//console.log('OnError:' + e);
console.log('OnError:' + e);
}
}

Loading…
Cancel
Save