Skip to content

Commit c83d231

Browse files
authored
Merge pull request #20 from wuhewuhe/master
add ping interval and maxAlive time option
2 parents 78765b2 + 3a13a13 commit c83d231

File tree

6 files changed

+97
-23
lines changed

6 files changed

+97
-23
lines changed

account.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,24 @@ func (s *AccountClient) GetMMPState(ctx context.Context, opts ...RequestOption)
180180
return res, nil
181181
}
182182

183+
func (s *AccountClient) SetSpotHedgeMode(ctx context.Context, opts ...RequestOption) (res *ServerResponse, err error) {
184+
r := &request{
185+
method: http.MethodGet,
186+
endpoint: "/v5/account/set-hedging-mode",
187+
secType: secTypeSigned,
188+
}
189+
data, err := s.c.callAPI(ctx, r, opts...)
190+
if err != nil {
191+
return nil, err
192+
}
193+
res = new(ServerResponse)
194+
err = json.Unmarshal(data, res)
195+
if err != nil {
196+
return nil, err
197+
}
198+
return res, nil
199+
}
200+
183201
func (s *AccountClient) UpgradeToUTA(ctx context.Context, opts ...RequestOption) (res *ServerResponse, err error) {
184202
r := &request{
185203
method: http.MethodPost,

broker.go

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,29 @@ func (s *BrokerServiceClient) GetBrokerEarning(ctx context.Context, opts ...Requ
1818
}
1919
r := &request{
2020
method: http.MethodGet,
21-
endpoint: "/v5/broker/earning-record",
21+
endpoint: "/v5/broker/earnings-info",
22+
secType: secTypeSigned,
23+
}
24+
r.setParams(s.params)
25+
data, err := s.c.callAPI(ctx, r, opts...)
26+
if err != nil {
27+
return nil, err
28+
}
29+
res = new(ServerResponse)
30+
err = json.Unmarshal(data, res)
31+
if err != nil {
32+
return nil, err
33+
}
34+
return res, nil
35+
}
36+
37+
func (s *BrokerServiceClient) GetBrokerAccountInfo(ctx context.Context, opts ...RequestOption) (res *ServerResponse, err error) {
38+
if err = handlers.ValidateParams(s.params); err != nil {
39+
return nil, err
40+
}
41+
r := &request{
42+
method: http.MethodGet,
43+
endpoint: "/v5/broker/account-info",
2244
secType: secTypeSigned,
2345
}
2446
r.setParams(s.params)

bybit_api_client.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ type doFunc func(req *http.Request) (*http.Response, error)
4040

4141
type ClientOption func(*Client)
4242

43+
// WithDebug print more details in debug mode
4344
func WithDebug(debug bool) ClientOption {
4445
return func(c *Client) {
4546
c.Debug = debug

bybit_websocket.go

Lines changed: 54 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -36,31 +36,68 @@ func (b *WebSocket) SetMessageHandler(handler MessageHandler) {
3636
}
3737

3838
type WebSocket struct {
39-
conn *websocket.Conn
40-
url string
41-
apiKey string
42-
apiSecret string
43-
onMessage MessageHandler
39+
conn *websocket.Conn
40+
url string
41+
apiKey string
42+
apiSecret string
43+
maxAliveTime string
44+
pingInterval int
45+
onMessage MessageHandler
4446
}
4547

46-
func NewBybitPrivateWebSocket(url, apiKey, apiSecret string, handler MessageHandler) *WebSocket {
47-
return &WebSocket{
48-
url: url,
49-
apiKey: apiKey,
50-
apiSecret: apiSecret,
51-
onMessage: handler,
48+
type WebsocketOption func(*WebSocket)
49+
50+
func WithPingInterval(pingInterval int) WebsocketOption {
51+
return func(c *WebSocket) {
52+
c.pingInterval = pingInterval
53+
}
54+
}
55+
56+
func WithMaxAliveTime(maxAliveTime string) WebsocketOption {
57+
return func(c *WebSocket) {
58+
c.maxAliveTime = maxAliveTime
5259
}
5360
}
5461

55-
func NewBybitPublicWebSocket(url string, handler MessageHandler) *WebSocket {
56-
return &WebSocket{
57-
url: url,
58-
onMessage: handler,
62+
func NewBybitPrivateWebSocket(url, apiKey, apiSecret string, handler MessageHandler, options ...WebsocketOption) *WebSocket {
63+
c := &WebSocket{
64+
url: url,
65+
apiKey: apiKey,
66+
apiSecret: apiSecret,
67+
maxAliveTime: "",
68+
pingInterval: 20,
69+
onMessage: handler,
5970
}
71+
72+
// Apply the provided options
73+
for _, opt := range options {
74+
opt(c)
75+
}
76+
77+
return c
78+
}
79+
80+
func NewBybitPublicWebSocket(url string, pingInterval int, handler MessageHandler, options ...WebsocketOption) *WebSocket {
81+
c := &WebSocket{
82+
url: url,
83+
pingInterval: pingInterval, // default is 20 seconds
84+
onMessage: handler,
85+
}
86+
87+
// Apply the provided options
88+
for _, opt := range options {
89+
opt(c)
90+
}
91+
92+
return c
6093
}
6194

6295
func (b *WebSocket) Connect(args []string) error {
6396
var err error
97+
wssUrl := b.url
98+
if b.maxAliveTime != "" {
99+
wssUrl += "?max_alive_time=" + b.maxAliveTime
100+
}
64101
b.conn, _, err = websocket.DefaultDialer.Dial(b.url, nil)
65102
if err != nil {
66103
return err
@@ -80,7 +117,7 @@ func (b *WebSocket) Connect(args []string) error {
80117
}
81118

82119
func Ping(b *WebSocket) {
83-
ticker := time.NewTicker(15 * time.Second)
120+
ticker := time.NewTicker(time.Duration(b.pingInterval) * time.Second)
84121
go func() {
85122
defer ticker.Stop() // Ensure the ticker is stopped when this goroutine ends
86123
for {
@@ -123,7 +160,7 @@ func (b *WebSocket) sendAuth() error {
123160
fmt.Println("signature generated : " + signature)
124161

125162
authMessage := map[string]interface{}{
126-
"req_id": uuid.New(), // You would need to implement or find a package for generating GUID
163+
"req_id": uuid.New(),
127164
"op": "auth",
128165
"args": []interface{}{b.apiKey, expires, signature},
129166
}

consts.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package bybit_connector
22

33
const (
44
Name = "bybit.api.go"
5-
Version = "1.0.0"
5+
Version = "1.0.1"
66
// Https
77
MAINNET = "https://api.bybit.com"
88
MAINNET_BACKT = "https://api.bytick.com"

go.sum

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
1-
github.com/bitly/go-simplejson v0.5.1/go.mod h1:YOPVLzCfwK14b4Sff3oP1AmGhI9T9Vsg84etUnlyp+Q=
21
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
32
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
43
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
54
github.com/google/uuid v1.4.0 h1:MtMxsa51/r9yyhkyLsVeVt0B+BGQZzpQiTQ4eHZ8bc4=
65
github.com/google/uuid v1.4.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
7-
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
8-
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
96
github.com/gorilla/websocket v1.5.1 h1:gmztn0JnHVt9JZquRuzLw3g4wouNVzKL15iLr/zn/QY=
107
github.com/gorilla/websocket v1.5.1/go.mod h1:x3kM2JMyaluk02fnUJpQuwD2dCS5NDG2ZHL0uE0tcaY=
118
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
129
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
1310
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
1411
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
15-
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
1612
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
1713
github.com/stretchr/objx v0.5.1 h1:4VhoImhV/Bm0ToFkXFi8hXNXwpDRZ/ynw3amt82mzq0=
1814
github.com/stretchr/objx v0.5.1/go.mod h1:/iHQpkQwBD6DLUmQ4pE+s1TXdob1mORJ4/UFdrifcy0=

0 commit comments

Comments
 (0)