| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061 |
- import 'dotenv/config'
- import { AsterWsClient } from '../src/exchanges/aster/wsClient'
- import { loadAsterConfig, toAsterWsConfig, toAsterAuthConfig } from '../src/config/asterConfig'
- async function main() {
- console.log('🚀 开始 Aster WebSocket 示例...\n')
- try {
- // 从环境变量加载配置
- const config = loadAsterConfig()
- const wsConfig = toAsterWsConfig(config)
- const authConfig = toAsterAuthConfig(config)
- console.log('📋 配置信息:')
- console.log(` WS URL: ${config.ws.url}`)
- console.log(` User: ${config.auth.user}`)
- console.log(` Signer: ${config.auth.signer}`)
- console.log(` 订阅交易对: ${config.subscribe.symbols.join(', ')}`)
- console.log('')
- const client = new AsterWsClient(wsConfig, authConfig)
- client.on('open', () => console.log('Aster WS connected'))
- client.on('close', (c, r) => console.log('Aster WS closed', c, r))
- client.on('error', e => console.error('Aster WS error', e))
- client.on('ticker', t => console.log('ticker', t))
- client.on('trade', t => console.log('trade', t))
- client.on('depth', d => console.log('depth', d.symbol, d.bids.length, d.asks.length))
- client.on('kline', k => console.log('kline', k.symbol, k.interval, k.close))
- client.connect()
- // 订阅:根据配置的交易对进行订阅(按照 Aster 文档格式)
- const subscriptions: any[] = []
- for (const symbol of config.subscribe.symbols) {
- subscriptions.push(
- { channel: 'aggTrade', symbol }, // 归集交易
- { channel: 'markPrice', symbol }, // 标记价格
- { channel: 'depth', symbol, depth: 20 }, // 深度数据
- )
- }
- client.subscribe(subscriptions)
- // 运行 10 秒后退出
- setTimeout(() => {
- try {
- client.unsubscribe(subscriptions)
- client.disconnect()
- console.log('🎉 Aster WebSocket 示例完成!')
- } finally {
- process.exit(0)
- }
- }, 10000)
- } catch (error) {
- console.error('❌ 配置加载失败:', error)
- process.exit(1)
- }
- }
- main().catch(console.error)
|