@@ -30,11 +30,13 @@ type WS struct {
3030 cid string // 客户端请求唯一ID
3131 accessKey string
3232 secretKey string
33+ debugMode bool
3334 subscriptions map [string ]interface {}
3435
35- tickerCallback func (trade * WSTicker )
36- depthCallback func (depth * WSDepth )
37- tradeCallback func (trade * WSTrade )
36+ tickerCallback func (trade * WSTicker )
37+ depthCallback func (depth * WSDepth )
38+ depthHFCallback func (depth * WSDepthHF )
39+ tradeCallback func (trade * WSTrade )
3840}
3941
4042// SetProxy 设置代理地址
@@ -68,7 +70,20 @@ func (ws *WS) SubscribeTicker(id string, symbol string) {
6870func (ws * WS ) SubscribeDepth (id string , symbol string ) {
6971 ch := map [string ]interface {}{
7072 "id" : id ,
71- "sub" : fmt .Sprintf ("market.%s.depth.step0" , symbol )}
73+ "sub" : fmt .Sprintf ("market.%s.depth.step0" , symbol ),
74+ }
75+ ws .Subscribe (id , ch )
76+ }
77+
78+ // SubscribeDepthHF 订阅增量深度
79+ // size: 20/150 档位数,20:表示20档不合并的深度,150:表示150档不合并的深度
80+ // dateType: 数据类型,不填默认为全量数据,"incremental":增量数据,"snapshot":全量数据
81+ func (ws * WS ) SubscribeDepthHF (id string , symbol string , size int , dateType string ) {
82+ ch := map [string ]interface {}{
83+ "id" : id ,
84+ "sub" : fmt .Sprintf ("market.%v.depth.size_%v.high_freq" , symbol , size ),
85+ "data_type" : dateType ,
86+ }
7287 ws .Subscribe (id , ch )
7388}
7489
@@ -99,6 +114,10 @@ func (ws *WS) SetDepthCallback(callback func(depth *WSDepth)) {
99114 ws .depthCallback = callback
100115}
101116
117+ func (ws * WS ) SetDepthHFCallback (callback func (depth * WSDepthHF )) {
118+ ws .depthHFCallback = callback
119+ }
120+
102121func (ws * WS ) SetTradeCallback (callback func (trade * WSTrade )) {
103122 ws .tradeCallback = callback
104123}
@@ -167,7 +186,9 @@ func (ws *WS) run() {
167186func (ws * WS ) handleMsg (messageType int , msg []byte ) {
168187 ret := gjson .ParseBytes (msg )
169188
170- //log.Printf("%v", string(msg))
189+ if ws .debugMode {
190+ log .Printf ("%v" , string (msg ))
191+ }
171192
172193 if pingValue := ret .Get ("ping" ); pingValue .Exists () {
173194 // 心跳
@@ -180,10 +201,22 @@ func (ws *WS) handleMsg(messageType int, msg []byte) {
180201 // {"id":"depth_1","subbed":"market.BTC_CQ.depth.step0","ts":1586498957314,"status":"ok"}
181202
182203 if chValue := ret .Get ("ch" ); chValue .Exists () {
183- // market.BTC_CQ.depth.step0
184204 ch := chValue .String ()
185205 if strings .HasPrefix (ch , "market" ) {
186- if strings .Contains (ch , ".depth" ) {
206+ if strings .HasSuffix (ch , ".high_freq" ) {
207+ // market.BTC_CQ.depth.size_20.high_freq
208+ var depth WSDepthHF
209+ err := json .Unmarshal (msg , & depth )
210+ if err != nil {
211+ log .Printf ("%v" , err )
212+ return
213+ }
214+
215+ if ws .depthHFCallback != nil {
216+ ws .depthHFCallback (& depth )
217+ }
218+ } else if strings .Contains (ch , ".depth" ) {
219+ // market.BTC_CQ.depth.step0
187220 var depth WSDepth
188221 err := json .Unmarshal (msg , & depth )
189222 if err != nil {
@@ -242,12 +275,13 @@ func (ws *WS) handlePing(ping int64) {
242275// wsURL:
243276// 正式地址 wss://api.hbdm.com/ws
244277// 开发地址 wss://api.btcgateway.pro/ws
245- func NewWS (wsURL string , accessKey string , secretKey string ) * WS {
278+ func NewWS (wsURL string , accessKey string , secretKey string , debugMode bool ) * WS {
246279 ws := & WS {
247280 wsURL : wsURL ,
248281 cid : shortuuid .New (),
249282 accessKey : accessKey ,
250283 secretKey : secretKey ,
284+ debugMode : debugMode ,
251285 subscriptions : make (map [string ]interface {}),
252286 }
253287 ws .ctx , ws .cancel = context .WithCancel (context .Background ())
0 commit comments