mirror of
https://github.com/d0zingcat/gocryptotrader.git
synced 2026-05-13 23:16:45 +00:00
* Websocket: Rename stream package * Websocket: Rename Websocket to Manager * Websocket: Replace explicit errs with common.NilGuard * Websocket: Move websocket_types.go to types.go * Websocket: Minor field comment and alignment in types * Webosocket: Rename WebsocketConnection to Connection * Alphapoint: Make gorilla ws import explicit Just to avoid confusion with our own packages. * Websocket: Move stream_match to match * Websocket: Move websocket_connection to connection * Websocket: Move websocket.go to manager.go * Websocket: Break out all subscription methods into subscriptions.go * Websocket: Move connection type into its file * Websocket: Remove PositionUpdated Type is not used anywhere * Kraken: Use local constant for pong Was the only use of websocket.Pong and doesn't really feel right to represent kraken's api resp in one of our packages * Websocket: Move connection sub-types to connection package * Websocket: Move manager types into manager * Websocket: Move ConnectionWrapper into manager * Websocket: Move websocket_test to manager_test * Websocket: Privatise connectionWrapper * Websocket: Remaining types into types.go These really belong somewhere else mostly, but this will do for now * Websocket: Tidy up connection method vars * Gofumpt: Moving package imports around * Websocket: Rename errDuplicateConnectionSetup * Websocket: Fix duplicate import of gws * Websocket: Fix gofumpt -extra * Websocket: Standardise import of gws across other pkgs * Kraken: Remove unused sub conf consts These were replaced by the generic Levels and Depth fields on all subs * Websocket: Privitise ConnectioWrapper fields * Websocket: inline single use var WebsocketNotAuthenticatedUsingRest * Websocket: Move documentation to template * Bithumb: Assertify TestWsHandleData
90 lines
3.0 KiB
Go
90 lines
3.0 KiB
Go
package bybit
|
|
|
|
import (
|
|
"context"
|
|
"net/http"
|
|
"strconv"
|
|
|
|
gws "github.com/gorilla/websocket"
|
|
"github.com/thrasher-corp/gocryptotrader/currency"
|
|
"github.com/thrasher-corp/gocryptotrader/encoding/json"
|
|
"github.com/thrasher-corp/gocryptotrader/exchanges/asset"
|
|
"github.com/thrasher-corp/gocryptotrader/exchanges/request"
|
|
"github.com/thrasher-corp/gocryptotrader/exchanges/subscription"
|
|
"github.com/thrasher-corp/gocryptotrader/internal/exchange/websocket"
|
|
)
|
|
|
|
// WsOptionsConnect connects to options a websocket feed
|
|
func (by *Bybit) WsOptionsConnect() error {
|
|
if !by.Websocket.IsEnabled() || !by.IsEnabled() || !by.IsAssetWebsocketSupported(asset.Options) {
|
|
return websocket.ErrWebsocketNotEnabled
|
|
}
|
|
by.Websocket.Conn.SetURL(optionPublic)
|
|
var dialer gws.Dialer
|
|
err := by.Websocket.Conn.Dial(&dialer, http.Header{})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
pingMessage := PingMessage{Operation: "ping", RequestID: strconv.FormatInt(by.Websocket.Conn.GenerateMessageID(false), 10)}
|
|
pingData, err := json.Marshal(pingMessage)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
by.Websocket.Conn.SetupPingHandler(request.Unset, websocket.PingHandler{
|
|
MessageType: gws.TextMessage,
|
|
Message: pingData,
|
|
Delay: bybitWebsocketTimer,
|
|
})
|
|
|
|
by.Websocket.Wg.Add(1)
|
|
go by.wsReadData(asset.Options, by.Websocket.Conn)
|
|
return nil
|
|
}
|
|
|
|
// GenerateOptionsDefaultSubscriptions generates default subscription
|
|
func (by *Bybit) GenerateOptionsDefaultSubscriptions() (subscription.List, error) {
|
|
var subscriptions subscription.List
|
|
channels := []string{chanOrderbook, chanPublicTrade, chanPublicTicker}
|
|
pairs, err := by.GetEnabledPairs(asset.Options)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
for z := range pairs {
|
|
for x := range channels {
|
|
subscriptions = append(subscriptions,
|
|
&subscription.Subscription{
|
|
Channel: channels[x],
|
|
Pairs: currency.Pairs{pairs[z]},
|
|
Asset: asset.Options,
|
|
})
|
|
}
|
|
}
|
|
return subscriptions, nil
|
|
}
|
|
|
|
// OptionSubscribe sends a subscription message to options public channels.
|
|
func (by *Bybit) OptionSubscribe(channelSubscriptions subscription.List) error {
|
|
return by.handleOptionsPayloadSubscription("subscribe", channelSubscriptions)
|
|
}
|
|
|
|
// OptionUnsubscribe sends an unsubscription messages through options public channels.
|
|
func (by *Bybit) OptionUnsubscribe(channelSubscriptions subscription.List) error {
|
|
return by.handleOptionsPayloadSubscription("unsubscribe", channelSubscriptions)
|
|
}
|
|
|
|
func (by *Bybit) handleOptionsPayloadSubscription(operation string, channelSubscriptions subscription.List) error {
|
|
payloads, err := by.handleSubscriptions(operation, channelSubscriptions)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
for a := range payloads {
|
|
// The options connection does not send the subscription request id back with the subscription notification payload
|
|
// therefore the code doesn't wait for the response to check whether the subscription is successful or not.
|
|
err = by.Websocket.Conn.SendJSONMessage(context.TODO(), request.Unset, payloads[a])
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|