forked from thrasher-corp/gocryptotrader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
huobiwebsocket.go
87 lines (74 loc) · 2.19 KB
/
huobiwebsocket.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
package main
import (
"fmt"
"log"
"github.com/thrasher-/socketio"
)
const (
HUOBI_SOCKETIO_ADDRESS = "https://hq.huobi.com:443"
HUOBI_SOCKET_SYMBOL_LIST = "reqSymbolList"
HUOBI_SOCKET_SYMBOL_DETAIL = "reqSymbolDetail"
HUOBI_SOCKET_SUBSCRIBE = "reqMsgSubscribe"
HUOBI_SOCKET_UNSUBSCRIBE = "reqMsgUnsubscribe"
HUOBI_SOCKET_TIMELINE = "reqTimeLine"
HUOBI_SOCKET_KLINE = "reqKLine"
HUOBI_SOCKET_DEPTH = "reqMarketDepth"
HUOBI_SOCKET_DEPTH_TOP = "reqMarketDepthTop"
HUOBI_SOCKET_TRADE_DETAIL_TOP = "reqTradeDetailTop"
HUOBI_SOCKET_MARKET_DETAIL = "reqMarketDetail"
)
var HuobiSocket *socketio.SocketIO
type HuobiRequest struct {
Version int `json:"version"`
MsgType string `json:"msgType"`
}
type HuobiResponse struct {
Version int `json:"version"`
MsgType string `json:"msgType"`
RequestIndex int64 `json:"requestIndex"`
RetCode int64 `json:"retCode"`
RetMessage string `json:"retMsg"`
Payload map[string]interface{} `json:"payload"`
}
func (h *HUOBI) OnConnect(output chan socketio.Message) {
if h.Verbose {
log.Printf("%s Connected to Websocket.", h.GetName())
}
msg := HuobiRequest{1, HUOBI_SOCKET_SYMBOL_LIST}
result, err := JSONEncode(msg)
if err != nil {
log.Println(err)
}
output <- socketio.CreateMessageEvent("request", string(result), nil)
}
func (h *HUOBI) OnDisconnect(output chan socketio.Message) {
log.Println("Disconnected from websocket client.. Reconnecting")
h.WebsocketClient()
}
func (h *HUOBI) OnMessage(message []byte, output chan socketio.Message) {
log.Println(string(message))
}
func (h *HUOBI) OnRequest(message []byte, output chan socketio.Message) {
response := HuobiResponse{}
err := JSONDecode(message, &response)
if err != nil {
log.Println(err)
}
log.Println(response)
}
func (h *HUOBI) WebsocketClient() {
events := make(map[string]func(message []byte, output chan socketio.Message))
events["request"] = h.OnRequest
HuobiSocket = &socketio.SocketIO{
OnConnect: h.OnConnect,
OnDisconnect: h.OnDisconnect,
OnMessage: h.OnMessage,
OnEvent: events,
}
err := socketio.ConnectToSocket(HUOBI_SOCKETIO_ADDRESS, HuobiSocket)
if err != nil {
fmt.Println(err)
return
}
log.Printf("%s Websocket client disconnected.", h.GetName())
}