forked from thrasher-corp/gocryptotrader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
btccwebsocket.go
149 lines (127 loc) · 3.62 KB
/
btccwebsocket.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 (
BTCC_SOCKETIO_ADDRESS = "https://websocket.btcc.com"
)
type BTCCWebsocketOrder struct {
Price float64 `json:"price"`
TotalAmount float64 `json:"totalamount"`
Type string `json:"type"`
}
type BTCCWebsocketGroupOrder struct {
Asks []BTCCWebsocketOrder `json:"ask"`
Bids []BTCCWebsocketOrder `json:"bid"`
Market string `json:"market"`
}
type BTCCWebsocketTrade struct {
Amount float64 `json:"amount"`
Date float64 `json:"date"`
Market string `json:"market"`
Price float64 `json:"price"`
TradeID float64 `json:"trade_id"`
Type string `json:"type"`
}
type BTCCWebsocketTicker 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 BTCCSocket *socketio.SocketIO
func (b *BTCC) 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, BTCCSocket.Version)
}
}
}
func (b *BTCC) OnDisconnect(output chan socketio.Message) {
log.Printf("%s Disconnected from websocket server.. Reconnecting.\n", b.GetName())
b.WebsocketClient()
}
func (b *BTCC) OnError() {
log.Printf("%s Error with Websocket connection.. Reconnecting.\n", b.GetName())
b.WebsocketClient()
}
func (b *BTCC) 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 *BTCC) OnTicker(message []byte, output chan socketio.Message) {
type Response struct {
Ticker BTCCWebsocketTicker `json:"ticker"`
}
var resp Response
err := JSONDecode(message, &resp)
if err != nil {
log.Println(err)
return
}
}
func (b *BTCC) OnGroupOrder(message []byte, output chan socketio.Message) {
type Response struct {
GroupOrder BTCCWebsocketGroupOrder `json:"grouporder"`
}
var resp Response
err := JSONDecode(message, &resp)
if err != nil {
log.Println(err)
return
}
}
func (b *BTCC) OnTrade(message []byte, output chan socketio.Message) {
trade := BTCCWebsocketTrade{}
err := JSONDecode(message, &trade)
if err != nil {
log.Println(err)
return
}
}
func (b *BTCC) WebsocketClient() {
events := make(map[string]func(message []byte, output chan socketio.Message))
events["grouporder"] = b.OnGroupOrder
events["ticker"] = b.OnTicker
events["trade"] = b.OnTrade
BTCCSocket = &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(BTCC_SOCKETIO_ADDRESS, BTCCSocket)
if err != nil {
log.Printf("%s Unable to connect to Websocket. Err: %s\n", b.GetName(), err)
continue
}
log.Printf("%s Disconnected from Websocket.\n", b.GetName())
}
}