mirror of
https://github.com/NoFxAiOS/nofx.git
synced 2026-07-18 18:04:32 +08:00
feat: implement coinank free kline api and kline websocket (#1281)
- implement coinank free kline api in coinank_api.Kline - implement coinank free kline ws in coinank_api.KlineWs. if needKline is true, kline data read from KlineCh. if needTicker is true, tickers data read from TickersCh.
This commit is contained in:
60
provider/coinank/coinank_api/klinw_ws_test.go
Normal file
60
provider/coinank/coinank_api/klinw_ws_test.go
Normal file
@@ -0,0 +1,60 @@
|
||||
package coinank_api
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"nofx/provider/coinank/coinank_enum"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func TestKlineWs(t *testing.T) {
|
||||
ctx := context.TODO()
|
||||
ws, err := WsConn(ctx, true, true)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
go func() {
|
||||
for tickers := range ws.TickersCh {
|
||||
msg, err := json.Marshal(tickers)
|
||||
if err != nil {
|
||||
fmt.Println("json err:", err)
|
||||
}
|
||||
fmt.Println(string(msg))
|
||||
}
|
||||
fmt.Println("tickersCh closed")
|
||||
}()
|
||||
go func() {
|
||||
for kline := range ws.KlineCh {
|
||||
msg, err := json.Marshal(kline)
|
||||
if err != nil {
|
||||
fmt.Println("json err:", err)
|
||||
}
|
||||
fmt.Println(string(msg))
|
||||
}
|
||||
fmt.Println("kline closed")
|
||||
}()
|
||||
err = ws.Subscribe("BTCUSDT", coinank_enum.Binance, coinank_enum.Minute1)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
fmt.Println("sub success")
|
||||
time.Sleep(10 * time.Second)
|
||||
err = ws.UnSubscribe("BTCUSDT", coinank_enum.Binance, coinank_enum.Minute1)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
fmt.Println("unsub success")
|
||||
time.Sleep(10 * time.Second)
|
||||
err = ws.Subscribe("BTCUSDT", coinank_enum.Binance, coinank_enum.Hour1)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
fmt.Println("resub success")
|
||||
time.Sleep(10 * time.Second)
|
||||
ws.Close()
|
||||
fmt.Println("cancel success")
|
||||
time.Sleep(10 * time.Second)
|
||||
fmt.Println("all success")
|
||||
}
|
||||
Reference in New Issue
Block a user