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. 4
      helpers/clients.js
  2. 20
      helpers/index.js
  3. 11
      lib/client.js

4
helpers/clients.js

@ -17,6 +17,10 @@ class Clients {
bitcoin: [], bitcoin: [],
bitcoinTestnet: [] bitcoinTestnet: []
}; };
this.subscribedHeaders = {
bitcoin: false,
bitcoinTestnet: false,
};
} }
updateNetwork(network) { updateNetwork(network) {

20
helpers/index.js

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

11
lib/client.js

@ -139,17 +139,11 @@ class Client {
} }
onMessage(body, n) { onMessage(body, n) {
const msg = JSON.parse(body);
if (msg instanceof Array) {
this.response(msg);
} else {
if (msg.id !== void 0) {
try { try {
const msg = JSON.parse(body); const msg = JSON.parse(body);
if (msg instanceof Array) { if (msg instanceof Array) {
this.response(msg); this.response(msg);
} else { } else {
this.subscribe.emit(msg.method, msg.params);
if (msg.id !== void 0) { if (msg.id !== void 0) {
this.response(msg); this.response(msg);
} else { } else {
@ -162,13 +156,12 @@ class Client {
this.onClose(error); this.onClose(error);
} }
} }
}
}
onConnect(){ onConnect(){
} }
onClose(){ onClose(){
this.status = 0;
Object.keys(this.callback_message_queue).forEach((key) => { Object.keys(this.callback_message_queue).forEach((key) => {
this.callback_message_queue[key](new Error('close connect')) this.callback_message_queue[key](new Error('close connect'))
delete this.callback_message_queue[key] delete this.callback_message_queue[key]
@ -180,7 +173,7 @@ class Client {
} }
onError(e){ onError(e){
//console.log('OnError:' + e); console.log('OnError:' + e);
} }
} }

Loading…
Cancel
Save