Skip to content

Commit 418ff24

Browse files
authored
update README.md and add the way to set up websocket proxy. (#571)
* update READMD.md and add introduction about proxy; replace options with eoptions, just change the package name add the way to manually set up the websocket proxy, current websocket proxy got from environment variable * fix typos * change eoptions as options;resolved review * correct base url of options' websocket * revert v2/client.go * change eoptions as options in v2/client.go * revert v2/client.go
1 parent 5824a29 commit 418ff24

9 files changed

+121
-6
lines changed

README.md

+25-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,12 @@ Name | Description | Status
2121
[web-socket-streams.md](https://github.com/binance/binance-spot-api-docs/blob/master/web-socket-streams.md) | Details on available streams and payloads | <input type="checkbox" checked> Implemented
2222
[user-data-stream.md](https://github.com/binance/binance-spot-api-docs/blob/master/user-data-stream.md) | Details on the dedicated account stream | <input type="checkbox" checked> Implemented
2323
[margin-api.md](https://binance-docs.github.io/apidocs/spot/en) | Details on the Margin API (/sapi) | <input type="checkbox" checked> Implemented
24-
[futures-api.md](https://binance-docs.github.io/apidocs/futures/en/#general-info) | Details on the Futures API (/fapi) | <input type="checkbox" checked> Partially Implemented
25-
[delivery-api.md](https://binance-docs.github.io/apidocs/delivery/en/#general-info) | Details on the Coin-M Futures API (/dapi) | <input type="checkbox" checked> Partially Implemented
24+
[futures-api.md](https://binance-docs.github.io/apidocs/futures/en/#general-info) | Details on the Futures API (/fapi) | <input type="checkbox" checked> Implemented
25+
[delivery-api.md](https://binance-docs.github.io/apidocs/delivery/en/#general-info) | Details on the Coin-M Futures API (/dapi) | <input type="checkbox" checked> Implemented
26+
[options-api.md](https://binance-docs.github.io/apidocs/voptions/en/#general-info) | Detains on the Options API(/eapi) | <input type="checkbox" checked> Implemented
27+
28+
29+
If you find an unimplemented interface, please submit an issue.
2630

2731
### Installation
2832

@@ -40,7 +44,12 @@ go get github.com/adshao/go-binance/v1
4044

4145
```golang
4246
import (
47+
// for spot and other interfaces contained in https://binance-docs.github.io/apidocs/spot/en/#change-log
4348
"github.com/adshao/go-binance/v2"
49+
50+
"github.com/adshao/go-binance/v2/futures" // optional package
51+
"github.com/adshao/go-binance/v2/delivery" // optional package
52+
"github.com/adshao/go-binance/v2/options" // optional package
4453
)
4554
```
4655

@@ -72,6 +81,14 @@ Following are some simple examples, please refer to [godoc](https://godoc.org/gi
7281

7382
If you have any questions, please refer to the specific version of the code for specific reference definitions or usage methods
7483

84+
##### Proxy Client
85+
86+
```
87+
proxyUrl := "http://127.0.0.1:7890" // Please replace it with your exact proxy URL.
88+
client := binance.NewProxiedClient(apiKey, apiSecret, proxyUrl)
89+
```
90+
91+
7592
#### Create Order
7693

7794
```golang
@@ -221,6 +238,12 @@ You don't need Client in websocket API. Just call binance.WsXxxServe(args, handl
221238

222239
> For delivery API you can use `delivery.WsXxxServe(args, handler, errHandler)`.
223240
241+
If you want to use a proxy, you can set `HTTPS_PROXY` or `HTTP_PROXY` in the environment variable, or you can call `SetWsProxyUrl` in the target packages within your code. Then you can call other websocket functions. For example:
242+
```golang
243+
binance.SetWsProxyUrl("http://127.0.0.1:7890")
244+
binance.WsDepthServe("LTCBTC", wsDepthHandler, errHandler)
245+
```
246+
224247
#### Depth
225248

226249
```golang

v2/delivery/websocket.go

+12-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package delivery
22

33
import (
44
"net/http"
5+
"net/url"
56
"time"
67

78
"github.com/gorilla/websocket"
@@ -16,17 +17,27 @@ type ErrHandler func(err error)
1617
// WsConfig webservice configuration
1718
type WsConfig struct {
1819
Endpoint string
20+
Proxy *string
1921
}
2022

2123
func newWsConfig(endpoint string) *WsConfig {
2224
return &WsConfig{
2325
Endpoint: endpoint,
26+
Proxy: getWsProxyUrl(),
2427
}
2528
}
2629

2730
var wsServe = func(cfg *WsConfig, handler WsHandler, errHandler ErrHandler) (doneC, stopC chan struct{}, err error) {
31+
proxy := http.ProxyFromEnvironment
32+
if cfg.Proxy != nil {
33+
u, err := url.Parse(*cfg.Proxy)
34+
if err != nil {
35+
return nil, nil, err
36+
}
37+
proxy = http.ProxyURL(u)
38+
}
2839
Dialer := websocket.Dialer{
29-
Proxy: http.ProxyFromEnvironment,
40+
Proxy: proxy,
3041
HandshakeTimeout: 45 * time.Second,
3142
EnableCompression: false,
3243
}

v2/delivery/websocket_service.go

+12
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ var (
2121
WebsocketKeepalive = false
2222
// UseTestnet switch all the WS streams from production to the testnet
2323
UseTestnet = false
24+
ProxyUrl = ""
2425
)
2526

2627
// getWsEndpoint return the base endpoint of the WS according the UseTestnet flag
@@ -31,6 +32,17 @@ func getWsEndpoint() string {
3132
return baseWsMainUrl
3233
}
3334

35+
func getWsProxyUrl() *string {
36+
if ProxyUrl == "" {
37+
return nil
38+
}
39+
return &ProxyUrl
40+
}
41+
42+
func SetWsProxyUrl(url string) {
43+
ProxyUrl = url
44+
}
45+
3446
// WsAggTradeEvent define websocket aggTrde event.
3547
type WsAggTradeEvent struct {
3648
Event string `json:"e"`

v2/futures/websocket.go

+12-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package futures
22

33
import (
44
"net/http"
5+
"net/url"
56
"time"
67

78
"github.com/gorilla/websocket"
@@ -16,17 +17,27 @@ type ErrHandler func(err error)
1617
// WsConfig webservice configuration
1718
type WsConfig struct {
1819
Endpoint string
20+
Proxy *string
1921
}
2022

2123
func newWsConfig(endpoint string) *WsConfig {
2224
return &WsConfig{
2325
Endpoint: endpoint,
26+
Proxy: getWsProxyUrl(),
2427
}
2528
}
2629

2730
var wsServe = func(cfg *WsConfig, handler WsHandler, errHandler ErrHandler) (doneC, stopC chan struct{}, err error) {
31+
proxy := http.ProxyFromEnvironment
32+
if cfg.Proxy != nil {
33+
u, err := url.Parse(*cfg.Proxy)
34+
if err != nil {
35+
return nil, nil, err
36+
}
37+
proxy = http.ProxyURL(u)
38+
}
2839
Dialer := websocket.Dialer{
29-
Proxy: http.ProxyFromEnvironment,
40+
Proxy: proxy,
3041
HandshakeTimeout: 45 * time.Second,
3142
EnableCompression: false,
3243
}

v2/futures/websocket_service.go

+12
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,20 @@ var (
2323
WebsocketKeepalive = false
2424
// UseTestnet switch all the WS streams from production to the testnet
2525
UseTestnet = false
26+
ProxyUrl = ""
2627
)
2728

29+
func getWsProxyUrl() *string {
30+
if ProxyUrl == "" {
31+
return nil
32+
}
33+
return &ProxyUrl
34+
}
35+
36+
func SetWsProxyUrl(url string) {
37+
ProxyUrl = url
38+
}
39+
2840
// getWsEndpoint return the base endpoint of the WS according the UseTestnet flag
2941
func getWsEndpoint() string {
3042
if UseTestnet {

v2/options/websocket.go

+13-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package options
22

33
import (
44
"net/http"
5+
"net/url"
56
"time"
67

78
"github.com/gorilla/websocket"
@@ -16,17 +17,28 @@ type ErrHandler func(err error)
1617
// WsConfig webservice configuration
1718
type WsConfig struct {
1819
Endpoint string
20+
Proxy *string
1921
}
2022

2123
func newWsConfig(endpoint string) *WsConfig {
2224
return &WsConfig{
2325
Endpoint: endpoint,
26+
Proxy: getWsProxyUrl(),
2427
}
2528
}
2629

2730
var wsServe = func(cfg *WsConfig, handler WsHandler, errHandler ErrHandler) (doneC, stopC chan struct{}, err error) {
31+
proxy := http.ProxyFromEnvironment
32+
if cfg.Proxy != nil {
33+
u, err := url.Parse(*cfg.Proxy)
34+
if err != nil {
35+
return nil, nil, err
36+
}
37+
proxy = http.ProxyURL(u)
38+
}
39+
2840
Dialer := websocket.Dialer{
29-
Proxy: http.ProxyFromEnvironment,
41+
Proxy: proxy,
3042
HandshakeTimeout: 45 * time.Second,
3143
EnableCompression: false,
3244
}

v2/options/websocket_service.go

+11
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,17 @@ func getWsEndpoint() string {
3535
return baseWsMainUrl
3636
}
3737

38+
func getWsProxyUrl() *string {
39+
if ProxyUrl == "" {
40+
return nil
41+
}
42+
return &ProxyUrl
43+
}
44+
45+
func SetWsProxyUrl(url string) {
46+
ProxyUrl = url
47+
}
48+
3849
// getCombinedEndpoint return the base endpoint of the combined stream according the UseTestnet flag
3950
func getCombinedEndpoint() string {
4051
if UseTestnet {

v2/websocket.go

+12-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package binance
22

33
import (
44
"net/http"
5+
"net/url"
56
"time"
67

78
"github.com/gorilla/websocket"
@@ -16,17 +17,27 @@ type ErrHandler func(err error)
1617
// WsConfig webservice configuration
1718
type WsConfig struct {
1819
Endpoint string
20+
Proxy *string
1921
}
2022

2123
func newWsConfig(endpoint string) *WsConfig {
2224
return &WsConfig{
2325
Endpoint: endpoint,
26+
Proxy: getWsProxyUrl(),
2427
}
2528
}
2629

2730
var wsServe = func(cfg *WsConfig, handler WsHandler, errHandler ErrHandler) (doneC, stopC chan struct{}, err error) {
31+
proxy := http.ProxyFromEnvironment
32+
if cfg.Proxy != nil {
33+
u, err := url.Parse(*cfg.Proxy)
34+
if err != nil {
35+
return nil, nil, err
36+
}
37+
proxy = http.ProxyURL(u)
38+
}
2839
Dialer := websocket.Dialer{
29-
Proxy: http.ProxyFromEnvironment,
40+
Proxy: proxy,
3041
HandshakeTimeout: 45 * time.Second,
3142
EnableCompression: false,
3243
}

v2/websocket_service.go

+12
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,20 @@ var (
1818
WebsocketTimeout = time.Second * 60
1919
// WebsocketKeepalive enables sending ping/pong messages to check the connection stability
2020
WebsocketKeepalive = false
21+
ProxyUrl = ""
2122
)
2223

24+
func getWsProxyUrl() *string {
25+
if ProxyUrl == "" {
26+
return nil
27+
}
28+
return &ProxyUrl
29+
}
30+
31+
func SetWsProxyUrl(url string) {
32+
ProxyUrl = url
33+
}
34+
2335
// getWsEndpoint return the base endpoint of the WS according the UseTestnet flag
2436
func getWsEndpoint() string {
2537
if UseTestnet {

0 commit comments

Comments
 (0)