forked from cosmos/gex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
websocket.go
60 lines (45 loc) · 1.25 KB
/
websocket.go
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
package main
// example websocket connection to blocks
import (
"log"
"os"
"os/signal"
"github.com/sacOO7/gowebsocket"
)
// func main() {
// websocket()
// }
func websocket() {
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt)
socket := gowebsocket.New("ws://localhost:26657/websocket")
socket.OnConnectError = func(err error, socket gowebsocket.Socket) {
log.Fatal("Received connect error - ", err)
}
socket.OnConnected = func(socket gowebsocket.Socket) {
log.Println("Connected to server")
}
socket.OnTextMessage = func(message string, socket gowebsocket.Socket) {
log.Println("Received message - " + message)
}
socket.OnPingReceived = func(data string, socket gowebsocket.Socket) {
log.Println("Received ping - " + data)
}
socket.OnPongReceived = func(data string, socket gowebsocket.Socket) {
log.Println("Received pong - " + data)
}
socket.OnDisconnected = func(err error, socket gowebsocket.Socket) {
log.Println("Disconnected from server ")
return
}
socket.Connect()
socket.SendText("{ \"jsonrpc\": \"2.0\", \"method\": \"subscribe\", \"params\": [\"tm.event='NewBlock'\"], \"id\": 1 }")
for {
select {
case <-interrupt:
log.Println("interrupt")
socket.Close()
return
}
}
}