127 lines
		
	
	
		
			3.9 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			127 lines
		
	
	
		
			3.9 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| package bitgetservice
 | ||
| 
 | ||
| import "time"
 | ||
| 
 | ||
| // SubscribeReq 订阅请求结构(基于官方SDK)
 | ||
| type SubscribeReq struct {
 | ||
| 	InstType string `json:"instType"` // 产品类型:SPOT-现货,UMCBL-U本位合约,DMCBL-币本位合约
 | ||
| 	Channel  string `json:"channel"`  // 频道名称
 | ||
| 	InstId   string `json:"instId"`   // 产品ID
 | ||
| 	// Coin     string `json:"coin"`     // 币种(可选)
 | ||
| }
 | ||
| 
 | ||
| // WsBaseReq WebSocket基础请求结构
 | ||
| type WsBaseReq struct {
 | ||
| 	Op   string        `json:"op"`   // 操作类型:subscribe, unsubscribe, login
 | ||
| 	Args []interface{} `json:"args"` // 参数列表
 | ||
| }
 | ||
| 
 | ||
| // WsLoginReq WebSocket登录请求
 | ||
| type WsLoginReq struct {
 | ||
| 	ApiKey     string `json:"apiKey"`
 | ||
| 	Passphrase string `json:"passphrase"`
 | ||
| 	Timestamp  string `json:"timestamp"`
 | ||
| 	Sign       string `json:"sign"`
 | ||
| }
 | ||
| 
 | ||
| // OnReceive 消息接收回调函数类型
 | ||
| type OnReceive func(message string)
 | ||
| 
 | ||
| // Set 集合数据结构(简化实现)
 | ||
| type Set struct {
 | ||
| 	items map[SubscribeReq]bool
 | ||
| }
 | ||
| 
 | ||
| // NewSet 创建新的集合
 | ||
| func NewSet() *Set {
 | ||
| 	return &Set{
 | ||
| 		items: make(map[SubscribeReq]bool),
 | ||
| 	}
 | ||
| }
 | ||
| 
 | ||
| // Add 添加元素到集合
 | ||
| func (s *Set) Add(item SubscribeReq) {
 | ||
| 	s.items[item] = true
 | ||
| }
 | ||
| 
 | ||
| // Remove 从集合中移除元素
 | ||
| func (s *Set) Remove(item SubscribeReq) {
 | ||
| 	delete(s.items, item)
 | ||
| }
 | ||
| 
 | ||
| // Contains 检查集合是否包含元素
 | ||
| func (s *Set) Contains(item SubscribeReq) bool {
 | ||
| 	_, exists := s.items[item]
 | ||
| 	return exists
 | ||
| }
 | ||
| 
 | ||
| // BitgetMarketData 市场数据基础结构
 | ||
| type BitgetMarketData struct {
 | ||
| 	Action string       `json:"action"` // 推送类型
 | ||
| 	Arg    SubscribeReq `json:"arg"`    // 订阅参数
 | ||
| 	Data   interface{}  `json:"data"`   // 数据内容
 | ||
| 	Ts     int64        `json:"ts"`     // 时间戳
 | ||
| }
 | ||
| 
 | ||
| // TickerData 行情数据
 | ||
| type TickerData struct {
 | ||
| 	InstId    string `json:"instId"`    // 产品ID
 | ||
| 	Last      string `json:"last"`      // 最新价
 | ||
| 	Open24h   string `json:"open24h"`   // 24小时开盘价
 | ||
| 	High24h   string `json:"high24h"`   // 24小时最高价
 | ||
| 	Low24h    string `json:"low24h"`    // 24小时最低价
 | ||
| 	Change24h string `json:"change24h"` // 24小时涨跌幅
 | ||
| 	Vol24h    string `json:"vol24h"`    // 24小时成交量
 | ||
| 	VolCcy24h string `json:"volCcy24h"` // 24小时成交额
 | ||
| 	Ts        string `json:"ts"`        // 时间戳
 | ||
| }
 | ||
| 
 | ||
| // DepthData 深度数据
 | ||
| type DepthData struct {
 | ||
| 	InstId string     `json:"instId"` // 产品ID
 | ||
| 	Asks   [][]string `json:"asks"`   // 卖盘 [价格, 数量]
 | ||
| 	Bids   [][]string `json:"bids"`   // 买盘 [价格, 数量]
 | ||
| 	Ts     string     `json:"ts"`     // 时间戳
 | ||
| }
 | ||
| 
 | ||
| // TradeData 成交数据
 | ||
| type TradeData struct {
 | ||
| 	InstId  string `json:"instId"`  // 产品ID
 | ||
| 	TradeId string `json:"tradeId"` // 成交ID
 | ||
| 	Px      string `json:"px"`      // 成交价格
 | ||
| 	Sz      string `json:"sz"`      // 成交数量
 | ||
| 	Side    string `json:"side"`    // 成交方向 buy/sell
 | ||
| 	Ts      string `json:"ts"`      // 成交时间
 | ||
| }
 | ||
| 
 | ||
| // KlineData K线数据
 | ||
| type KlineData struct {
 | ||
| 	InstId string   `json:"instId"` // 产品ID
 | ||
| 	Candle []string `json:"candle"` // K线数据 [时间戳, 开盘价, 最高价, 最低价, 收盘价, 成交量, 成交额]
 | ||
| 	Ts     string   `json:"ts"`     // 时间戳
 | ||
| }
 | ||
| 
 | ||
| // BitgetMarketConfig 市场数据配置
 | ||
| type BitgetMarketConfig struct {
 | ||
| 	WsUrl             string        // WebSocket地址
 | ||
| 	ReconnectInterval time.Duration // 重连间隔
 | ||
| 	PingInterval      time.Duration // Ping间隔
 | ||
| 	ReadTimeout       time.Duration // 读取超时
 | ||
| 	WriteTimeout      time.Duration // 写入超时
 | ||
| 	MaxReconnectTimes int           // 最大重连次数
 | ||
| 	EnableCompression bool          // 是否启用压缩
 | ||
| }
 | ||
| 
 | ||
| // DefaultMarketConfig 默认市场数据配置
 | ||
| func DefaultMarketConfig() *BitgetMarketConfig {
 | ||
| 	return &BitgetMarketConfig{
 | ||
| 		WsUrl:             "wss://ws.bitget.com/v2/ws/public",
 | ||
| 		ReconnectInterval: 5 * time.Second,
 | ||
| 		PingInterval:      15 * time.Second,
 | ||
| 		ReadTimeout:       60 * time.Second,
 | ||
| 		WriteTimeout:      10 * time.Second,
 | ||
| 		MaxReconnectTimes: 10,
 | ||
| 		EnableCompression: false,
 | ||
| 	}
 | ||
| }
 |