Last active
July 8, 2020 22:46
-
-
Save gloriousCode/32078a7d5d43bfeb7572e6584d9623f5 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// AddSubscription appends a successful subscription | |
func (w *Websocket) AddSubscription(cs ...ChannelSubscription) { | |
w.subscriptions = append(w.subscriptions, cs...) | |
} | |
// Demos how it can be handled when only one request sent | |
func (b *Bitmex) Subscribe(channelsToSubscribe []stream.ChannelSubscription) error { | |
var subscriber WebsocketRequest | |
subscriber.Command = "subscribe" | |
for i := range channelsToSubscribe { | |
subscriber.Arguments = append(subscriber.Arguments, | |
channelsToSubscribe[i].Channel) | |
} | |
err := b.Websocket.Conn.SendJSONMessage(subscriber) | |
if err != nil { | |
return err | |
} | |
b.Websocket.AddSubscription(channelsToSubscribe...) | |
return nil | |
} | |
// Demos how it can be handled when errors can occur per generic send | |
func (b *Bitstamp) Subscribe(channelsToSubscribe []stream.ChannelSubscription) error { | |
var errs common.Errors | |
for i := range channelsToSubscribe { | |
req := websocketEventRequest{ | |
Event: "bts:subscribe", | |
Data: websocketData{ | |
Channel: channelsToSubscribe[i].Channel, | |
}, | |
} | |
err := b.Websocket.Conn.SendJSONMessage(req) | |
if err != nil { | |
errs = append(errs, err) | |
continue | |
} | |
b.Websocket.AddSubscription(channelsToSubscribe[i]) | |
} | |
return errs | |
} | |
// Demo for more complicated Kraken | |
func (k *Kraken) Subscribe(channelsToSubscribe []stream.ChannelSubscription) error { | |
var errs common.Errors | |
var subs []WebsocketSubscriptionEventRequest | |
channels: | |
for x := range channelsToSubscribe { | |
for y := range subs { | |
if subs[y].Subscription.Name == channelsToSubscribe[x].Channel { | |
subs[y].Pairs = append(subs[y].Pairs, | |
channelsToSubscribe[x].Currency.String()) | |
continue channels | |
} | |
} | |
var id int64 | |
if common.StringDataContains(authenticatedChannels, channelsToSubscribe[x].Channel) { | |
id = k.Websocket.AuthConn.GenerateMessageID(false) | |
} else { | |
id = k.Websocket.Conn.GenerateMessageID(false) | |
} | |
resp := WebsocketSubscriptionEventRequest{ | |
Event: krakenWsSubscribe, | |
Subscription: WebsocketSubscriptionData{ | |
Name: channelsToSubscribe[x].Channel, | |
}, | |
RequestID: id, | |
} | |
if channelsToSubscribe[x].Channel == "book" { | |
// TODO: Add ability to make depth customisable | |
resp.Subscription.Depth = 1000 | |
} | |
if !channelsToSubscribe[x].Currency.IsEmpty() { | |
resp.Pairs = []string{channelsToSubscribe[x].Currency.String()} | |
} | |
if channelsToSubscribe[x].Params != nil { | |
resp.Subscription.Token = authToken | |
} | |
subs = append(subs, resp) | |
} | |
for i := range subs { | |
if common.StringDataContains(authenticatedChannels, subs[i].Subscription.Name) { | |
_, err := k.Websocket.AuthConn.SendMessageReturnResponse(subs[i].RequestID, subs[i]) | |
if err != nil { | |
errs = append(errs, err) | |
} | |
k.Websocket.AddSubscription(channelsToSubscribe[i]) | |
continue | |
} | |
_, err := k.Websocket.Conn.SendMessageReturnResponse(subs[i].RequestID, subs[i]) | |
if err != nil { | |
errs = append(errs, err) | |
} | |
k.Websocket.AddSubscription(channelsToSubscribe[i]) | |
} | |
return errs | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment