Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I want to subscribe to multiple Kline data at the same time #593

Open
shuyabin opened this issue Jul 22, 2024 · 3 comments
Open

I want to subscribe to multiple Kline data at the same time #593

shuyabin opened this issue Jul 22, 2024 · 3 comments

Comments

@shuyabin
Copy link

shuyabin commented Jul 22, 2024

How should a handler subscribe to multiple Kline data simultaneously
eth_usdt, 1m , btc,5m
WsKlineServe("ETHBTC", "1m", func(event *WsKlineEvent) { }

@xyq-c-cpp
Copy link
Collaborator

Multiple calls to WSKlineService if there don't have a interface to do that. @shuyabin

@vlw
Copy link
Contributor

vlw commented Oct 14, 2024

baseURL     = "wss://fstream.binance.com/stream?streams="

func getStreamURL(cl []string) string {
	var streams []string
	for _, coin := range cl {
		streams = append(streams, fmt.Sprintf("%s@kline_1m", coin))
	}
	return baseURL + strings.Join(streams, "/")
}
import "github.com/coder/websocket"

wsClient, resp, err := websocket.Dial(ctx, getStreamURL(cList),
	&websocket.DialOptions{
		CompressionMode: websocket.CompressionContextTakeover,
	},
)

...

messageType, content, err := conn.Read(ctx)
if messageType != websocket.MessageText {
	continue
}

...

j, err := newJSON(content)

jData := j.Get("data")

type WsCustomEvent struct {
	Event  string          `json:"e"`
	Symbol string          `json:"s"`
	Kline  futures.WsKline `json:"k"`
}

event := new(WsCustomEvent)
event.Event = jData.Get("e").MustString()
event.Symbol = jData.Get("s").MustString()

if event.Event == "kline" {
	item := jData.Get("k")
	event.Kline = futures.WsKline{
		Symbol:    item.Get("s").MustString(),
		High:      item.Get("h").MustString(),
		...
	}
}

		

@vlw
Copy link
Contributor

vlw commented Oct 14, 2024

The code works, but it would be nice to do this in the library itself

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants