forked from tensam/gocryptotrader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
btcchinawebsocket.go
149 lines (127 loc) · 3.7 KB
/
btcchinawebsocket.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
138
139
140
141
142
143
144
145
146
147
148
149
package main
import (
"fmt"
"github.com/thrasher-/socketio"
"log"
)
const (
BTCCHINA_SOCKETIO_ADDRESS = "https://websocket.btcchina.com"
)
type BTCChinaWebsocketOrder struct {
Price float64 `json:"price"`
TotalAmount float64 `json:"totalamount"`
Type string `json:"type"`
}
type BTCChinaWebsocketGroupOrder struct {
Asks []BTCChinaWebsocketOrder `json:"ask"`
Bids []BTCChinaWebsocketOrder `json:"bid"`
Market string `json:"market"`
}
type BTCChinaWebsocketTrade struct {
Amount float64 `json:"amount,string"`
Date float64 `json:"date"`
Market string `json:"market"`
Price float64 `json:"price,string"`
TradeID float64 `json:"trade_id"`
Type string `json:"type"`
}
type BTCChinaWebsocketTicker struct {
Buy float64 `json:"buy"`
Date float64 `json:"date"`
High float64 `json:"high"`
Last float64 `json:"last"`
Low float64 `json:"low"`
Market string `json:"market"`
Open float64 `json:"open"`
PrevClose float64 `json:"prev_close"`
Sell float64 `json:"sell"`
Volume float64 `json:"vol"`
Vwap float64 `json:"vwap"`
}
var BTCChinaSocket *socketio.SocketIO
func (b *BTCChina) OnConnect(output chan socketio.Message) {
if b.Verbose {
log.Printf("%s Connected to Websocket.", b.GetName())
}
currencies := []string{}
for _, x := range b.EnabledPairs {
currency := StringToLower(x[3:] + x[0:3])
currencies = append(currencies, currency)
}
endpoints := []string{"marketdata", "grouporder"}
for _, x := range endpoints {
for _, y := range currencies {
channel := fmt.Sprintf(`"%s_%s"`, x, y)
if b.Verbose {
log.Printf("%s Websocket subscribing to channel: %s.", b.GetName(), channel)
}
output <- socketio.CreateMessageEvent("subscribe", channel, b.OnMessage, BTCChinaSocket.Version)
}
}
}
func (b *BTCChina) OnDisconnect(output chan socketio.Message) {
log.Printf("%s Disconnected from websocket server.. Reconnecting.\n", b.GetName())
b.WebsocketClient()
}
func (b *BTCChina) OnError() {
log.Printf("%s Error with Websocket connection.. Reconnecting.\n", b.GetName())
b.WebsocketClient()
}
func (b *BTCChina) OnMessage(message []byte, output chan socketio.Message) {
if b.Verbose {
log.Printf("%s Websocket message received which isn't handled by default.\n", b.GetName())
log.Println(string(message))
}
}
func (b *BTCChina) OnTicker(message []byte, output chan socketio.Message) {
type Response struct {
Ticker BTCChinaWebsocketTicker `json:"ticker"`
}
var resp Response
err := JSONDecode(message, &resp)
if err != nil {
log.Println(err)
return
}
}
func (b *BTCChina) OnGroupOrder(message []byte, output chan socketio.Message) {
type Response struct {
GroupOrder BTCChinaWebsocketGroupOrder `json:"grouporder"`
}
var resp Response
err := JSONDecode(message, &resp)
if err != nil {
log.Println(err)
return
}
}
func (b *BTCChina) OnTrade(message []byte, output chan socketio.Message) {
trade := BTCChinaWebsocketTrade{}
err := JSONDecode(message, &trade)
if err != nil {
log.Println(err)
return
}
}
func (b *BTCChina) WebsocketClient() {
events := make(map[string]func(message []byte, output chan socketio.Message))
events["grouporder"] = b.OnGroupOrder
events["ticker"] = b.OnTicker
events["trade"] = b.OnTrade
BTCChinaSocket = &socketio.SocketIO{
Version: 1,
OnConnect: b.OnConnect,
OnEvent: events,
OnError: b.OnError,
OnMessage: b.OnMessage,
OnDisconnect: b.OnDisconnect,
}
for b.Enabled && b.Websocket {
err := socketio.ConnectToSocket(BTCCHINA_SOCKETIO_ADDRESS, BTCChinaSocket)
if err != nil {
log.Printf("%s Unable to connect to Websocket. Err: %s\n", err)
continue
}
log.Printf("%s Disconnected from Websocket.")
}
}