-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathblockchain-websockets.js
More file actions
66 lines (66 loc) · 2.18 KB
/
blockchain-websockets.js
File metadata and controls
66 lines (66 loc) · 2.18 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
58
59
60
61
62
63
64
65
66
Blockchain = function(){
this.url = 'wss://ws.blockchain.info/inv';
this.address_list = [];
this.WebSocketClient = Npm.require('websocket').client;
this.client = new this.WebSocketClient();
this.subscribeAllTransactions = function(){
var message = JSON.stringify({"op":"unconfirmed_sub"});
if (this.client.connection.connected) {
this.client.connection.sendUTF(message);
}
};
this.subscribeToAddressList = function () {
this.address_list.forEach(function(address){
var message = JSON.stringify({"op":"addr_sub", "addr":address});
if (this.client.connection.connected) {
this.client.connection.sendUTF(message);
}
});
};
this.addNewAddressSubscription = function(address){
this.address_list.push(address);
var message = JSON.stringify({"op":"addr_sub", "addr":address});
if (this.client.connection.connected) {
this.client.connection.sendUTF(message);
}
};
this.messageHandler = function(message){
if (message.type === 'utf8') {
console.log("> "+ message.utf8Data);
}
};
this.client.on('connectFailed', function(error) {
console.log('Connect Error: ' + error.toString());
});
var _this = this;
this.client.on('connect', function(connection) {
this.connection = connection;
console.log('WebSocket Client Connected');
connection.on('error', function(error) {
console.log("Connection Error: " + error.toString());
});
connection.on('close', function() {
console.log('echo-protocol Connection Closed');
this.connect('wss://ws.blockchain.info/inv');
this.subscribe();
});
connection.on('message', function(message) {
_this.messageHandler(message);
});
});
this.connect = function (){
this.client.connect(this.url)
};
this.testBlock = function (){
var message = JSON.stringify({"op":"ping_block"});
if (this.client.connection.connected) {
this.client.connection.sendUTF(message);
}
};
this.testTransaction = function (){
var message = JSON.stringify({"op":"ping_tx"});
if (this.client.connection.connected) {
this.client.connection.sendUTF(message);
}
};
};