-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
96 lines (77 loc) · 1.87 KB
/
index.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
'use strict';
const WebSocket = require('ws');
var Slack = require('slack-node');
const webhookUri = process.env['WEBHOOK_URI']
const divvydUri = process.env['DIVVYD_URI']
var slack = new Slack();
slack.setWebhook(webhookUri);
function messageSlack (message) {
slack.webhook({
text: message
}, function(err, response) {
if (err)
console.log(err)
});
}
var ledger = 0
var ws
function subscribe(ip) {
console.log('subscribing to', ip)
ws = new WebSocket(ip);
ws.on('error', function(error) {
ws.close();
console.log('error')
console.log(error)
});
ws.on('close', function(error) {
console.log('close error')
console.log(error)
});
function heartbeat() {
this.isAlive = true;
}
ws.on('open', function() {
ws.isAlive = true;
ws.on('pong', heartbeat);
ws.send(JSON.stringify({
id: 1,
command: 'subscribe',
streams: [
'transactions'
]
}), function (error) {
if (error) {
console.log('send error')
console.log(error)
}
});
});
ws.on('message', function(dataString) {
const data = JSON.parse(dataString);
if (data.type === 'transaction') {
if (data.transaction.TransactionType === 'EnableAmendment') {
console.log(data.transaction)
var message = '`' + data.transaction.TransactionType + '`\n```\n'
message += JSON.stringify(data.transaction, null, ' ')
message += '\n```'
messageSlack(message)
}
if (ledger < data.ledger_index) {
ledger = data.ledger_index
console.log(ledger)
}
} else {
console.log(data);
}
});
}
subscribe(divvydUri);
const interval = setInterval(function ping() {
if (ws.isAlive === false) {
console.log('reconnect')
ws.terminate();
subscribe(divvydUri);
}
ws.isAlive = false;
ws.ping('', true, true);
}, 5000);