Skip to content

Commit a276dd8

Browse files
committed
Update
1 parent a855619 commit a276dd8

5 files changed

Lines changed: 29 additions & 9 deletions

File tree

futures_ws.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ type FuturesWS struct {
2828
accessKey string
2929
secretKey string
3030
passphrase string
31+
debugMode bool
3132

3233
ctx context.Context
3334
cancel context.CancelFunc
@@ -235,6 +236,11 @@ func (ws *FuturesWS) run() {
235236

236237
func (ws *FuturesWS) handleMsg(messageType int, msg []byte) {
237238
ret := gjson.ParseBytes(msg)
239+
240+
if ws.debugMode {
241+
log.Printf("%v", string(msg))
242+
}
243+
238244
// 登录成功
239245
// {"event":"login","success":true}
240246

@@ -403,12 +409,13 @@ func (ws *FuturesWS) handleMsg(messageType int, msg []byte) {
403409
// NewFuturesWS 创建合约WS
404410
// wsURL:
405411
// wss://real.okex.com:8443/ws/v3
406-
func NewFuturesWS(wsURL string, accessKey string, secretKey string, passphrase string) *FuturesWS {
412+
func NewFuturesWS(wsURL string, accessKey string, secretKey string, passphrase string, debugMode bool) *FuturesWS {
407413
ws := &FuturesWS{
408414
wsURL: wsURL,
409415
accessKey: accessKey,
410416
secretKey: secretKey,
411417
passphrase: passphrase,
418+
debugMode: debugMode,
412419
subscriptions: make(map[string]interface{}),
413420
dobMap: make(map[string]*DepthOrderBook),
414421
}

futures_ws_test.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,12 @@ func newFuturesWSForTest() *FuturesWS {
2020
wsURL := "wss://real.okex.com:8443/ws/v3"
2121

2222
ws := NewFuturesWS(wsURL,
23-
accessKey, secretKey, passphrase)
23+
accessKey, secretKey, passphrase, true)
2424
err = ws.SetProxy("socks5://127.0.0.1:1080")
25-
ws.SetProxy("socks5://127.0.0.1:1080")
2625
//ws.SetProxy("http://127.0.0.1:1080")
27-
//if err != nil {
28-
// log.Fatal(err)
29-
//}
26+
if err != nil {
27+
log.Fatal(err)
28+
}
3029
return ws
3130
}
3231

@@ -59,3 +58,14 @@ func TestFuturesWS_Depth20(t *testing.T) {
5958

6059
select {}
6160
}
61+
62+
func TestFuturesWS_SubscribeOrder(t *testing.T) {
63+
ws := newFuturesWSForTest()
64+
ws.SetOrderCallback(func(orders []WSOrder) {
65+
log.Printf("%#v", orders)
66+
})
67+
ws.SubscribeOrder("order_1", "BTC-USD-200626")
68+
ws.Start()
69+
70+
select {}
71+
}

go.sum

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXf
118118
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
119119
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
120120
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
121+
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
121122
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
122123
github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s=
123124
github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw=

swap_ws.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ type SwapWS struct {
2828
accessKey string
2929
secretKey string
3030
passphrase string
31+
debugMode bool
3132

3233
ctx context.Context
3334
cancel context.CancelFunc
@@ -384,12 +385,13 @@ func (ws *SwapWS) handleMsg(messageType int, msg []byte) {
384385
// NewSwapWS 创建永续合约WS
385386
// wsURL:
386387
// wss://real.okex.com:8443/ws/v3
387-
func NewSwapWS(wsURL string, accessKey string, secretKey string, passphrase string) *SwapWS {
388+
func NewSwapWS(wsURL string, accessKey string, secretKey string, passphrase string, debugMode bool) *SwapWS {
388389
ws := &SwapWS{
389390
wsURL: wsURL,
390391
accessKey: accessKey,
391392
secretKey: secretKey,
392393
passphrase: passphrase,
394+
debugMode: debugMode,
393395
subscriptions: make(map[string]interface{}),
394396
dobMap: make(map[string]*DepthOrderBook),
395397
}

swap_ws_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"testing"
77
)
88

9-
func newSwapWSForTest() *FuturesWS {
9+
func newSwapWSForTest() *SwapWS {
1010
viper.SetConfigName("test_config")
1111
viper.AddConfigPath("./conf")
1212
err := viper.ReadInConfig()
@@ -18,7 +18,7 @@ func newSwapWSForTest() *FuturesWS {
1818
secretKey := viper.GetString("secret_key")
1919
passphrase := viper.GetString("passphrase")
2020
wsURL := "wss://real.okex.com:8443/ws/v3"
21-
ws := NewFuturesWS(wsURL, accessKey, secretKey, passphrase)
21+
ws := NewSwapWS(wsURL, accessKey, secretKey, passphrase, true)
2222
err = ws.SetProxy("socks5://127.0.0.1:1080")
2323
////ws.SetProxy("http://127.0.0.1:1080")
2424
if err != nil {

0 commit comments

Comments
 (0)