-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
137 lines (120 loc) · 2.74 KB
/
main.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
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package main
import (
"bytes"
"compress/gzip"
"encoding/json"
"io/ioutil"
"log"
"net/url"
"os"
"os/signal"
"time"
"github.com/gorilla/websocket"
)
var (
btcKlineTopic = "market.btcusdt.kline.1min"
eosKlineTopic = "market.eosusdt.kline.1min"
)
//SubRequest keyword to subscribe huobi api
type SubRequest struct {
Sub string `json:"sub"`
ID string `json:"id"`
FreqMs int `json:"freq-ms,omitempty"`
}
//Tick kline info
type Tick struct {
ID int `json:"id"`
Open float64 `json:"open"`
Close float64 `json:"close"`
Low float64 `json:"low"`
High float64 `json:"high"`
Amount float64 `json:"amount"`
Volume float64 `json:"vol"`
Count int `json:"count"`
}
//SubResponse huobi api response
type SubResponse struct {
Ch string `json:"ch"`
Ts int `json:"ts"`
Tick Tick `json:"tick"`
}
func gzipCompress(in []byte) ([]byte, error) {
reader, err := gzip.NewReader(bytes.NewReader(in))
if err != nil {
return nil, err
}
defer reader.Close()
msg, err := ioutil.ReadAll(reader)
if err != nil {
return nil, err
}
return msg, nil
}
func main() {
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt, os.Interrupt)
u := url.URL{Scheme: "wss", Host: "api.huobi.pro", Path: "/ws"}
log.Printf("connecting to %s", u.String())
c, _, err := websocket.DefaultDialer.Dial(u.String(), nil)
if err != nil {
log.Fatal("dial:", err)
}
defer c.Close()
subRequest := &SubRequest{Sub: btcKlineTopic, ID: "id1", FreqMs: 5000}
msg, err := json.Marshal(subRequest)
if err != nil {
log.Println(err)
}
err = c.WriteMessage(websocket.TextMessage, []byte(msg))
if err != nil {
log.Println("write", err)
}
done := make(chan struct{})
subRes := SubResponse{}
go func() {
defer close(done)
for {
_, message, err := c.ReadMessage()
if err != nil {
log.Println("read:", err)
return
}
msg, err := gzipCompress(message)
if err != nil {
log.Printf("%s", err)
}
err = json.Unmarshal(msg, &subRes)
if err == nil {
log.Printf("recv: %#v", subRes)
}
}
}()
ticker := time.NewTicker(time.Second)
defer ticker.Stop()
for {
select {
case <-done:
return
case t := <-ticker.C:
err := c.WriteMessage(websocket.TextMessage, []byte(t.String()))
if err != nil {
log.Println("write:", err)
return
}
case <-interrupt:
log.Println("interrupt")
// Cleanly close the connection by sending a close message and then
// waiting (with timeout) for the server to close the connection.
err := c.WriteMessage(websocket.CloseMessage, websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""))
if err != nil {
log.Println("write close:", err)
return
}
select {
case <-done:
case <-time.After(time.Second):
}
return
}
}
}