Skip to content

Commit

Permalink
update README.md and add the way to set up websocket proxy. (adshao#571)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
xyq-c-cpp authored Jun 20, 2024
1 parent 5824a29 commit 418ff24
Show file tree
Hide file tree
Showing 9 changed files with 121 additions and 6 deletions.
27 changes: 25 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,12 @@ Name | Description | Status
[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
[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
[margin-api.md](https://binance-docs.github.io/apidocs/spot/en) | Details on the Margin API (/sapi) | <input type="checkbox" checked> Implemented
[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
[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
[futures-api.md](https://binance-docs.github.io/apidocs/futures/en/#general-info) | Details on the Futures API (/fapi) | <input type="checkbox" checked> Implemented
[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
[options-api.md](https://binance-docs.github.io/apidocs/voptions/en/#general-info) | Detains on the Options API(/eapi) | <input type="checkbox" checked> Implemented


If you find an unimplemented interface, please submit an issue.

### Installation

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

```golang
import (
// for spot and other interfaces contained in https://binance-docs.github.io/apidocs/spot/en/#change-log
"github.com/adshao/go-binance/v2"

"github.com/adshao/go-binance/v2/futures" // optional package
"github.com/adshao/go-binance/v2/delivery" // optional package
"github.com/adshao/go-binance/v2/options" // optional package
)
```

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

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

##### Proxy Client

```
proxyUrl := "http://127.0.0.1:7890" // Please replace it with your exact proxy URL.
client := binance.NewProxiedClient(apiKey, apiSecret, proxyUrl)
```


#### Create Order

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

> For delivery API you can use `delivery.WsXxxServe(args, handler, errHandler)`.
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:
```golang
binance.SetWsProxyUrl("http://127.0.0.1:7890")
binance.WsDepthServe("LTCBTC", wsDepthHandler, errHandler)
```

#### Depth

```golang
Expand Down
13 changes: 12 additions & 1 deletion v2/delivery/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package delivery

import (
"net/http"
"net/url"
"time"

"github.com/gorilla/websocket"
Expand All @@ -16,17 +17,27 @@ type ErrHandler func(err error)
// WsConfig webservice configuration
type WsConfig struct {
Endpoint string
Proxy *string
}

func newWsConfig(endpoint string) *WsConfig {
return &WsConfig{
Endpoint: endpoint,
Proxy: getWsProxyUrl(),
}
}

var wsServe = func(cfg *WsConfig, handler WsHandler, errHandler ErrHandler) (doneC, stopC chan struct{}, err error) {
proxy := http.ProxyFromEnvironment
if cfg.Proxy != nil {
u, err := url.Parse(*cfg.Proxy)
if err != nil {
return nil, nil, err
}
proxy = http.ProxyURL(u)
}
Dialer := websocket.Dialer{
Proxy: http.ProxyFromEnvironment,
Proxy: proxy,
HandshakeTimeout: 45 * time.Second,
EnableCompression: false,
}
Expand Down
12 changes: 12 additions & 0 deletions v2/delivery/websocket_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ var (
WebsocketKeepalive = false
// UseTestnet switch all the WS streams from production to the testnet
UseTestnet = false
ProxyUrl = ""
)

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

func getWsProxyUrl() *string {
if ProxyUrl == "" {
return nil
}
return &ProxyUrl
}

func SetWsProxyUrl(url string) {
ProxyUrl = url
}

// WsAggTradeEvent define websocket aggTrde event.
type WsAggTradeEvent struct {
Event string `json:"e"`
Expand Down
13 changes: 12 additions & 1 deletion v2/futures/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package futures

import (
"net/http"
"net/url"
"time"

"github.com/gorilla/websocket"
Expand All @@ -16,17 +17,27 @@ type ErrHandler func(err error)
// WsConfig webservice configuration
type WsConfig struct {
Endpoint string
Proxy *string
}

func newWsConfig(endpoint string) *WsConfig {
return &WsConfig{
Endpoint: endpoint,
Proxy: getWsProxyUrl(),
}
}

var wsServe = func(cfg *WsConfig, handler WsHandler, errHandler ErrHandler) (doneC, stopC chan struct{}, err error) {
proxy := http.ProxyFromEnvironment
if cfg.Proxy != nil {
u, err := url.Parse(*cfg.Proxy)
if err != nil {
return nil, nil, err
}
proxy = http.ProxyURL(u)
}
Dialer := websocket.Dialer{
Proxy: http.ProxyFromEnvironment,
Proxy: proxy,
HandshakeTimeout: 45 * time.Second,
EnableCompression: false,
}
Expand Down
12 changes: 12 additions & 0 deletions v2/futures/websocket_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,20 @@ var (
WebsocketKeepalive = false
// UseTestnet switch all the WS streams from production to the testnet
UseTestnet = false
ProxyUrl = ""
)

func getWsProxyUrl() *string {
if ProxyUrl == "" {
return nil
}
return &ProxyUrl
}

func SetWsProxyUrl(url string) {
ProxyUrl = url
}

// getWsEndpoint return the base endpoint of the WS according the UseTestnet flag
func getWsEndpoint() string {
if UseTestnet {
Expand Down
14 changes: 13 additions & 1 deletion v2/options/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package options

import (
"net/http"
"net/url"
"time"

"github.com/gorilla/websocket"
Expand All @@ -16,17 +17,28 @@ type ErrHandler func(err error)
// WsConfig webservice configuration
type WsConfig struct {
Endpoint string
Proxy *string
}

func newWsConfig(endpoint string) *WsConfig {
return &WsConfig{
Endpoint: endpoint,
Proxy: getWsProxyUrl(),
}
}

var wsServe = func(cfg *WsConfig, handler WsHandler, errHandler ErrHandler) (doneC, stopC chan struct{}, err error) {
proxy := http.ProxyFromEnvironment
if cfg.Proxy != nil {
u, err := url.Parse(*cfg.Proxy)
if err != nil {
return nil, nil, err
}
proxy = http.ProxyURL(u)
}

Dialer := websocket.Dialer{
Proxy: http.ProxyFromEnvironment,
Proxy: proxy,
HandshakeTimeout: 45 * time.Second,
EnableCompression: false,
}
Expand Down
11 changes: 11 additions & 0 deletions v2/options/websocket_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,17 @@ func getWsEndpoint() string {
return baseWsMainUrl
}

func getWsProxyUrl() *string {
if ProxyUrl == "" {
return nil
}
return &ProxyUrl
}

func SetWsProxyUrl(url string) {
ProxyUrl = url
}

// getCombinedEndpoint return the base endpoint of the combined stream according the UseTestnet flag
func getCombinedEndpoint() string {
if UseTestnet {
Expand Down
13 changes: 12 additions & 1 deletion v2/websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package binance

import (
"net/http"
"net/url"
"time"

"github.com/gorilla/websocket"
Expand All @@ -16,17 +17,27 @@ type ErrHandler func(err error)
// WsConfig webservice configuration
type WsConfig struct {
Endpoint string
Proxy *string
}

func newWsConfig(endpoint string) *WsConfig {
return &WsConfig{
Endpoint: endpoint,
Proxy: getWsProxyUrl(),
}
}

var wsServe = func(cfg *WsConfig, handler WsHandler, errHandler ErrHandler) (doneC, stopC chan struct{}, err error) {
proxy := http.ProxyFromEnvironment
if cfg.Proxy != nil {
u, err := url.Parse(*cfg.Proxy)
if err != nil {
return nil, nil, err
}
proxy = http.ProxyURL(u)
}
Dialer := websocket.Dialer{
Proxy: http.ProxyFromEnvironment,
Proxy: proxy,
HandshakeTimeout: 45 * time.Second,
EnableCompression: false,
}
Expand Down
12 changes: 12 additions & 0 deletions v2/websocket_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,20 @@ var (
WebsocketTimeout = time.Second * 60
// WebsocketKeepalive enables sending ping/pong messages to check the connection stability
WebsocketKeepalive = false
ProxyUrl = ""
)

func getWsProxyUrl() *string {
if ProxyUrl == "" {
return nil
}
return &ProxyUrl
}

func SetWsProxyUrl(url string) {
ProxyUrl = url
}

// getWsEndpoint return the base endpoint of the WS according the UseTestnet flag
func getWsEndpoint() string {
if UseTestnet {
Expand Down

0 comments on commit 418ff24

Please sign in to comment.