-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
64 lines (55 loc) · 1.7 KB
/
app.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
var W3CWebSocket = require('websocket').w3cwebsocket;
var client = new W3CWebSocket('wss://ws.bitso.com', []);
client.onerror = function () {
console.log('Connection Error');
};
client.onopen = function () {
console.log('WebSocket Client Connected');
startApp();
};
client.onclose = function () {
console.log('echo-protocol Client Closed');
};
client.onmessage = function (message) {
var data = JSON.parse(message.data);
/**
* Trade Payload
* i = Integer: counter
* a = String: # BTC
* r = String: precio en MXN
* v = String: a * r
* t = Integer: (1 = tradeout | 0 = tradein)
* mo = String: ?
* to = String: ?
*/
if (data.type == 'trades' && data.payload) {
console.log("Received a tarde!");
for (var i in data.payload) {
var trade = data.payload[i];
var output = '[' + trade['i'] + '] ' + trade['a'] + ' BTC @ ' + trade['r'] + ' MXN = ' + trade['v'] + ' MXN';
console.log(output);
}
}
//else if (data.type == 'diff-orders' && data.payload) {
//
//}
//else if (data.type == 'orders' && data.payload) {
//
//}
//data.type == 'diff-orders'
//data.type == 'ka'
};
startApp = function () {
function subscribe() {
if (client.readyState === client.OPEN) {
client.send(JSON.stringify({
action: 'subscribe',
book: 'btc_mxn',
type: 'trades'
}));
//client.send(JSON.stringify({ action: 'subscribe', book: 'btc_mxn', type: 'diff-orders' }));
//client.send(JSON.stringify({ action: 'subscribe', book: 'btc_mxn', type: 'orders' }));
}
}
subscribe();
}