Merge pull request #20 from wuhewuhe/master
add ping interval and maxAlive time option
This commit is contained in:
commit
c83d23152d
18
account.go
18
account.go
|
@ -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,
|
||||
|
|
24
broker.go
24
broker.go
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -40,27 +40,64 @@ type WebSocket struct {
|
|||
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 NewBybitPublicWebSocket(url string, handler MessageHandler) *WebSocket {
|
||||
return &WebSocket{
|
||||
func WithMaxAliveTime(maxAliveTime string) WebsocketOption {
|
||||
return func(c *WebSocket) {
|
||||
c.maxAliveTime = maxAliveTime
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
|
@ -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 {
|
||||
|
@ -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},
|
||||
}
|
||||
|
|
|
@ -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"
|
||||
|
|
4
go.sum
4
go.sum
|
@ -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=
|
||||
|
|
Loading…
Reference in New Issue