forked from itzmeanjan/ette
-
Notifications
You must be signed in to change notification settings - Fork 0
/
block.js
33 lines (22 loc) · 884 Bytes
/
block.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
const { client } = require('websocket')
const _client = new client()
let state = true
_client.on('connectFailed', e => { console.error(`[!] Failed to connect : ${e}`); process.exit(1); })
// connect for listening to any block being mined
// event
_client.on('connect', c => {
c.on('close', d => {
console.log(`[!] Closed connection : ${d}`)
process.exit(0)
})
// receiving json encoded message
c.on('message', d => { console.log(JSON.parse(d.utf8Data)) })
// periodic subscription & unsubscription request performed
handler = _ => {
c.send(JSON.stringify({ name: 'block', type: state ? 'subscribe' : 'unsubscribe', apiKey: '0x43ee4523fc8cc569b492fcda6f32e07d90d2afdf7a8a02b5e74c933be1785487' }))
state = !state
}
setInterval(handler, 10000)
handler()
})
_client.connect('ws://localhost:7000/v1/ws', null)