This repository was archived by the owner on Jan 13, 2023. It is now read-only.
File tree 1 file changed +37
-0
lines changed
1 file changed +37
-0
lines changed Original file line number Diff line number Diff line change
1
+ const { client } = require ( 'websocket' )
2
+
3
+ const _client = new client ( )
4
+
5
+ let state = true
6
+
7
+ _client . on ( 'connectFailed' , e => { console . error ( `[!] Failed to connect : ${ e } ` ) ; process . exit ( 1 ) ; } )
8
+
9
+ // connect for listening to any block being mined
10
+ // event & any transaction being mined in any of those blocks
11
+ // & any event being emitted from contract interaction transactions
12
+ _client . on ( 'connect' , c => {
13
+ c . on ( 'close' , d => {
14
+ console . log ( `[!] Closed connection : ${ d } ` )
15
+ process . exit ( 0 )
16
+ } )
17
+
18
+ // receiving json encoded message
19
+ c . on ( 'message' , d => { console . log ( JSON . parse ( d . utf8Data ) ) } )
20
+
21
+ // periodic subscription & unsubscription request performed
22
+ handler = _ => {
23
+
24
+ c . send ( JSON . stringify ( { name : 'block' , type : state ? 'subscribe' : 'unsubscribe' , apiKey : '0xab84c27c721f54ef3ea63f1f75ee70f1f0a2863ec30df5a9f1f057ccbba5d4c3' } ) )
25
+ c . send ( JSON . stringify ( { name : 'transaction/*/*' , type : state ? 'subscribe' : 'unsubscribe' , apiKey : '0xab84c27c721f54ef3ea63f1f75ee70f1f0a2863ec30df5a9f1f057ccbba5d4c3' } ) )
26
+ c . send ( JSON . stringify ( { name : 'event/*/*/*/*/*' , type : state ? 'subscribe' : 'unsubscribe' , apiKey : '0xab84c27c721f54ef3ea63f1f75ee70f1f0a2863ec30df5a9f1f057ccbba5d4c3' } ) )
27
+
28
+ state = ! state
29
+
30
+ }
31
+
32
+ setInterval ( handler , 10000 )
33
+ handler ( )
34
+
35
+ } )
36
+
37
+ _client . connect ( 'ws://localhost:7000/v1/ws' , null )
You can’t perform that action at this time.
0 commit comments