@@ -10,13 +10,18 @@ import (
1010 "os"
1111 "sort"
1212 "strings"
13+ "sync"
1314 "time"
1415)
1516
1617type FuturesWs struct {
17- base * BinanceFutures
18- f * goex.WsConn
19- d * goex.WsConn
18+ base * BinanceFutures
19+ fOnce sync.Once
20+ dOnce sync.Once
21+
22+ wsBuilder * goex.WsBuilder
23+ f * goex.WsConn
24+ d * goex.WsConn
2025
2126 depthCallFn func (depth * goex.Depth )
2227 tickerCallFn func (ticker * goex.FutureTicker )
@@ -26,25 +31,44 @@ type FuturesWs struct {
2631func NewFuturesWs () * FuturesWs {
2732 futuresWs := new (FuturesWs )
2833
29- wsBuilder : = goex .NewWsBuilder ().
34+ futuresWs . wsBuilder = goex .NewWsBuilder ().
3035 ProxyUrl (os .Getenv ("HTTPS_PROXY" )).
3136 ProtoHandleFunc (futuresWs .handle ).AutoReconnect ()
32- futuresWs .f = wsBuilder .WsUrl ("wss://fstream.binance.com/ws" ).Build ()
33- futuresWs .d = wsBuilder .WsUrl ("wss://dstream.binance.com/ws" ).Build ()
34- futuresWs .base = NewBinanceFutures (& goex.APIConfig {
35- HttpClient : & http.Client {
37+
38+ httpCli := & http.Client {
39+ Timeout : 10 * time .Second ,
40+ }
41+
42+ if os .Getenv ("HTTPS_PROXY" ) != "" {
43+ httpCli = & http.Client {
3644 Transport : & http.Transport {
3745 Proxy : func (r * http.Request ) (* url.URL , error ) {
3846 return url .Parse (os .Getenv ("HTTPS_PROXY" ))
3947 },
4048 },
4149 Timeout : 10 * time .Second ,
42- },
50+ }
51+ }
52+
53+ futuresWs .base = NewBinanceFutures (& goex.APIConfig {
54+ HttpClient : httpCli ,
4355 })
4456
4557 return futuresWs
4658}
4759
60+ func (s * FuturesWs ) connectUsdtFutures () {
61+ s .fOnce .Do (func () {
62+ s .f = s .wsBuilder .WsUrl ("wss://fstream.binance.com/ws" ).Build ()
63+ })
64+ }
65+
66+ func (s * FuturesWs ) connectFutures () {
67+ s .dOnce .Do (func () {
68+ s .d = s .wsBuilder .WsUrl ("wss://dstream.binance.com/ws" ).Build ()
69+ })
70+ }
71+
4872func (s * FuturesWs ) DepthCallback (f func (depth * goex.Depth )) {
4973 s .depthCallFn = f
5074}
@@ -79,12 +103,14 @@ func (s *FuturesWs) SubscribeDepth(pair goex.CurrencyPair, contractType string)
79103func (s * FuturesWs ) SubscribeTicker (pair goex.CurrencyPair , contractType string ) error {
80104 switch contractType {
81105 case goex .SWAP_USDT_CONTRACT :
106+ s .connectUsdtFutures ()
82107 return s .f .Subscribe (req {
83108 Method : "SUBSCRIBE" ,
84109 Params : []string {pair .AdaptUsdToUsdt ().ToLower ().ToSymbol ("" ) + "@ticker" },
85110 Id : 1 ,
86111 })
87112 default :
113+ s .connectFutures ()
88114 sym , _ := s .base .adaptToSymbol (pair .AdaptUsdtToUsd (), contractType )
89115 return s .d .Subscribe (req {
90116 Method : "SUBSCRIBE" ,
0 commit comments