Skip to content

Commit

Permalink
add ping interval and maxAlive time option
Browse files Browse the repository at this point in the history
  • Loading branch information
VictorFrWu committed Dec 12, 2023
1 parent 3f46595 commit 3a13a13
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 23 deletions.
18 changes: 18 additions & 0 deletions account.go
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,24 @@ func (s *AccountClient) GetMMPState(ctx context.Context, opts ...RequestOption)
return res, nil
}

func (s *AccountClient) SetSpotHedgeMode(ctx context.Context, opts ...RequestOption) (res *ServerResponse, err error) {
r := &request{
method: http.MethodGet,
endpoint: "/v5/account/set-hedging-mode",
secType: secTypeSigned,
}
data, err := s.c.callAPI(ctx, r, opts...)
if err != nil {
return nil, err
}
res = new(ServerResponse)
err = json.Unmarshal(data, res)
if err != nil {
return nil, err
}
return res, nil
}

func (s *AccountClient) UpgradeToUTA(ctx context.Context, opts ...RequestOption) (res *ServerResponse, err error) {
r := &request{
method: http.MethodPost,
Expand Down
24 changes: 23 additions & 1 deletion broker.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,29 @@ func (s *BrokerServiceClient) GetBrokerEarning(ctx context.Context, opts ...Requ
}
r := &request{
method: http.MethodGet,
endpoint: "/v5/broker/earning-record",
endpoint: "/v5/broker/earnings-info",
secType: secTypeSigned,
}
r.setParams(s.params)
data, err := s.c.callAPI(ctx, r, opts...)
if err != nil {
return nil, err
}
res = new(ServerResponse)
err = json.Unmarshal(data, res)
if err != nil {
return nil, err
}
return res, nil
}

func (s *BrokerServiceClient) GetBrokerAccountInfo(ctx context.Context, opts ...RequestOption) (res *ServerResponse, err error) {
if err = handlers.ValidateParams(s.params); err != nil {
return nil, err
}
r := &request{
method: http.MethodGet,
endpoint: "/v5/broker/account-info",
secType: secTypeSigned,
}
r.setParams(s.params)
Expand Down
1 change: 1 addition & 0 deletions bybit_api_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type doFunc func(req *http.Request) (*http.Response, error)

type ClientOption func(*Client)

// WithDebug print more details in debug mode
func WithDebug(debug bool) ClientOption {
return func(c *Client) {
c.Debug = debug
Expand Down
71 changes: 54 additions & 17 deletions bybit_websocket.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,31 +36,68 @@ func (b *WebSocket) SetMessageHandler(handler MessageHandler) {
}

type WebSocket struct {
conn *websocket.Conn
url string
apiKey string
apiSecret string
onMessage MessageHandler
conn *websocket.Conn
url string
apiKey string
apiSecret string
maxAliveTime string
pingInterval int
onMessage MessageHandler
}

func NewBybitPrivateWebSocket(url, apiKey, apiSecret string, handler MessageHandler) *WebSocket {
return &WebSocket{
url: url,
apiKey: apiKey,
apiSecret: apiSecret,
onMessage: handler,
type WebsocketOption func(*WebSocket)

func WithPingInterval(pingInterval int) WebsocketOption {
return func(c *WebSocket) {
c.pingInterval = pingInterval
}
}

func WithMaxAliveTime(maxAliveTime string) WebsocketOption {
return func(c *WebSocket) {
c.maxAliveTime = maxAliveTime
}
}

func NewBybitPublicWebSocket(url string, handler MessageHandler) *WebSocket {
return &WebSocket{
url: url,
onMessage: handler,
func NewBybitPrivateWebSocket(url, apiKey, apiSecret string, handler MessageHandler, options ...WebsocketOption) *WebSocket {
c := &WebSocket{
url: url,
apiKey: apiKey,
apiSecret: apiSecret,
maxAliveTime: "",
pingInterval: 20,
onMessage: handler,
}

// Apply the provided options
for _, opt := range options {
opt(c)
}

return c
}

func NewBybitPublicWebSocket(url string, pingInterval int, handler MessageHandler, options ...WebsocketOption) *WebSocket {
c := &WebSocket{
url: url,
pingInterval: pingInterval, // default is 20 seconds
onMessage: handler,
}

// Apply the provided options
for _, opt := range options {
opt(c)
}

return c
}

func (b *WebSocket) Connect(args []string) error {
var err error
wssUrl := b.url
if b.maxAliveTime != "" {
wssUrl += "?max_alive_time=" + b.maxAliveTime
}
b.conn, _, err = websocket.DefaultDialer.Dial(b.url, nil)
if err != nil {
return err
Expand All @@ -80,7 +117,7 @@ func (b *WebSocket) Connect(args []string) error {
}

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

authMessage := map[string]interface{}{
"req_id": uuid.New(), // You would need to implement or find a package for generating GUID
"req_id": uuid.New(),
"op": "auth",
"args": []interface{}{b.apiKey, expires, signature},
}
Expand Down
2 changes: 1 addition & 1 deletion consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package bybit_connector

const (
Name = "bybit.api.go"
Version = "1.0.0"
Version = "1.0.1"
// Https
MAINNET = "https://api.bybit.com"
MAINNET_BACKT = "https://api.bytick.com"
Expand Down
4 changes: 0 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
github.com/bitly/go-simplejson v0.5.1/go.mod h1:YOPVLzCfwK14b4Sff3oP1AmGhI9T9Vsg84etUnlyp+Q=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/uuid v1.4.0 h1:MtMxsa51/r9yyhkyLsVeVt0B+BGQZzpQiTQ4eHZ8bc4=
github.com/google/uuid v1.4.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
github.com/gorilla/websocket v1.5.0/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/gorilla/websocket v1.5.1 h1:gmztn0JnHVt9JZquRuzLw3g4wouNVzKL15iLr/zn/QY=
github.com/gorilla/websocket v1.5.1/go.mod h1:x3kM2JMyaluk02fnUJpQuwD2dCS5NDG2ZHL0uE0tcaY=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/objx v0.5.1 h1:4VhoImhV/Bm0ToFkXFi8hXNXwpDRZ/ynw3amt82mzq0=
github.com/stretchr/objx v0.5.1/go.mod h1:/iHQpkQwBD6DLUmQ4pE+s1TXdob1mORJ4/UFdrifcy0=
Expand Down

0 comments on commit 3a13a13

Please sign in to comment.