59 lines
1.7 KiB
Go
59 lines
1.7 KiB
Go
package svc
|
||
|
||
import (
|
||
"blockchain/internal/domain/blockchain"
|
||
uc "blockchain/internal/domain/usecase"
|
||
repo "blockchain/internal/repository"
|
||
"blockchain/internal/usecase"
|
||
"time"
|
||
)
|
||
|
||
func InitBinanceKLineWebsocket() {
|
||
// 建立 adapter & handler
|
||
adapter := repo.NewBinanceAdapter(repo.BinanceAdapterParam{
|
||
Name: "Binance",
|
||
WsURL: "wss://fstream.binance.com/ws",
|
||
ClientPingInterval: 15 * time.Second,
|
||
ReadDeadline: 70 * time.Second,
|
||
})
|
||
handler := &usecase.PubHandler{}
|
||
|
||
cli := usecase.NewConnection(usecase.BinanceExchangeParam{
|
||
Adapter: adapter,
|
||
Handler: handler,
|
||
Backoff: time.Second,
|
||
MaxBackoff: 30 * time.Second,
|
||
Parallel: true,
|
||
ParallelN: 2048,
|
||
})
|
||
|
||
go Sub(cli, "BTCUSDT", "ETHUSDT")
|
||
// 這裡之後可以看要怎麼寫,改一下就可以訂閱更多,並且分連線,1024 個東西分一個連線
|
||
|
||
go cli.RunForever()
|
||
}
|
||
|
||
func Sub(cli uc.ExchangeConnect, pair ...string) {
|
||
_ = cli.SubscribeKLine(pair, blockchain.Interval1m)
|
||
time.Sleep(5 * time.Second)
|
||
_ = cli.SubscribeKLine(pair, blockchain.Interval3m)
|
||
time.Sleep(5 * time.Second)
|
||
_ = cli.SubscribeKLine(pair, blockchain.Interval5m)
|
||
time.Sleep(5 * time.Second)
|
||
_ = cli.SubscribeKLine(pair, blockchain.Interval15m)
|
||
time.Sleep(5 * time.Second)
|
||
_ = cli.SubscribeKLine(pair, blockchain.Interval30m)
|
||
time.Sleep(5 * time.Second)
|
||
_ = cli.SubscribeKLine(pair, blockchain.Interval1h)
|
||
time.Sleep(5 * time.Second)
|
||
_ = cli.SubscribeKLine(pair, blockchain.Interval4h)
|
||
time.Sleep(5 * time.Second)
|
||
_ = cli.SubscribeKLine(pair, blockchain.Interval1d)
|
||
time.Sleep(5 * time.Second)
|
||
_ = cli.SubscribeKLine(pair, blockchain.Interval1w)
|
||
time.Sleep(5 * time.Second)
|
||
_ = cli.SubscribeKLine(pair, blockchain.Interval1M)
|
||
|
||
return
|
||
}
|