forked from MaxLaumeister/bitlisten
-
Notifications
You must be signed in to change notification settings - Fork 0
/
socket.js
161 lines (126 loc) · 4.42 KB
/
socket.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
var satoshi = 100000000;
var lastBlockHeight = 0;
function TransactionSocket() {
}
TransactionSocket.init = function() {
// Terminate previous connection, if any
if (this.connection) this.connection.close();
if ('WebSocket' in window) {
var connection = new ReconnectingWebSocket('ws://ws.blockchain.info:8335/inv');
this.connection = connection;
StatusBox.reconnecting("blockchain");
connection.onopen = function() {
console.log('Blockchain.info: Connection open!');
StatusBox.connected("blockchain");
var newTransactions = {
"op" : "unconfirmed_sub"
};
var newBlocks = {
"op" : "blocks_sub"
};
connection.send(JSON.stringify(newTransactions));
connection.send(JSON.stringify(newBlocks));
connection.send(JSON.stringify({"op":"ping_tx"})); // Display the latest transaction so the user sees something.
}
connection.onclose = function() {
console.log('Blockchain.info: Connection closed');
if ($("#blockchainCheckBox").prop("checked")) StatusBox.reconnecting("blockchain");
else StatusBox.closed("blockchain");
}
connection.onerror = function(error) {
console.log('Blockchain.info: Connection Error: ' + error);
}
connection.onmessage = function(e) {
var data = JSON.parse(e.data);
// New Transaction
if (data.op == "utx") {
var transacted = 0;
for (var i = 0; i < data.x.out.length; i++) {
transacted += data.x.out[i].value;
}
var bitcoins = transacted / satoshi;
console.log("Transaction: " + bitcoins + " BTC");
var donation = false;
var outputs = data.x.out;
for (var i = 0; i < outputs.length; i++) {
if ((outputs[i].addr) == DONATION_ADDRESS) {
bitcoins = data.x.out[i].value / satoshi;
new Transaction(bitcoins, true);
return;
}
}
new Transaction(bitcoins);
}
else if (data.op == "block") {
var blockHeight = data.x.height;
var transactions = data.x.nTx;
var volumeSent = data.x.estimatedBTCSent;
var blockSize = data.x.size;
// Filter out the orphaned blocks.
if (blockHeight > lastBlockHeight) {
lastBlockHeight = blockHeight;
console.log("New Block");
new Block(blockHeight, transactions, volumeSent, blockSize);
}
}
}
} else {
//WebSockets are not supported.
console.log("No websocket support.");
StatusBox.nosupport("blockchain");
}
}
TransactionSocket.close = function() {
if (this.connection) this.connection.close();
StatusBox.closed("blockchain");
}
function TradeSocket() {
}
TradeSocket.init = function() {
// Terminate previous connection, if any
if (this.connection) this.connection.disconnect();
// Load Mtgox socket.io library
var self = this;
StatusBox.reconnecting("mtgox");
$.getScript("https://socketio.mtgox.com/socket.io/socket.io.js", function() {
// Make connection to Mtgox
var connection = io.connect('https://socketio.mtgox.com/mtgox');
self.connection = connection;
console.log("Opening Mtgox connection.");
connection.on('connect', function() {
console.log('Mtgox: Connection open!');
StatusBox.connected("mtgox");
// Unsubscribe from depth and ticker
connection.emit('message', {
"op" : "unsubscribe",
"channel" : "24e67e0d-1cad-4cc0-9e7a-f8523ef460fe"
});
connection.emit('message', {
"op" : "unsubscribe",
"channel" : "d5f06780-30a8-4a48-a2f8-7ed181b4a13f"
});
});
connection.on('disconnect', function() {
console.log('Mtgox: Connection closed');
if ($("#mtgoxCheckBox").prop("checked")) StatusBox.reconnecting("mtgox");
else StatusBox.closed("mtgox");
});
connection.on('error', function() {
console.log('Mtgox: Connection error.');
});
connection.on('message', function(message) {
if (message.trade) {
console.log("Trade: " + message.trade.amount_int / satoshi + " BTC | " + (message.trade.price * message.trade.amount_int / satoshi) + " " + message.trade.price_currency);
// 0.57 BTC | 42.75 USD
var bitcoins = message.trade.amount_int / satoshi;
var currency = (message.trade.price * message.trade.amount_int / satoshi);
var currencyName = message.trade.price_currency;
new Transaction(bitcoins, false, currency, currencyName);
}
});
});
}
TradeSocket.close = function() {
if (this.connection) this.connection.disconnect();
StatusBox.closed("mtgox");
}