WebSocket wss://{DOMAIN}/ws

WebSocket Real-time Data Push

Get real-time market data push through WebSocket connection. Supports subscribing to multiple trading pairs with real-time updates. API authentication required.

Connection Steps

  1. Establish a WebSocket connection to wss://{DOMAIN}/ws
  2. Send authentication message (including API Key and signature)
  3. Subscribe to trading pairs
  4. Receive real-time data push

Message Format

All messages are in JSON format.

Request Parameters

Response Example

{
    "认证响应": {
        "type": "auth",
        "status": "success",
        "message": "认证成功"
    },
    "订阅响应": {
        "type": "subscribe",
        "status": "success",
        "symbols": [
            "BTCUSDT",
            "ETHUSDT"
        ]
    },
    "数据推送": {
        "type": "ticker",
        "symbol": "BTCUSDT",
        "price": "45000.50",
        "change_24h": "2.5",
        "volume_24h": "1234567.89",
        "timestamp": 1701234567
    }
}

Code Example

// JavaScript WebSocket示例
const apiKey = "your_api_key";
const apiSecret = "your_api_secret";
const timestamp = Math.floor(Date.now() / 1000);

// 生成签名
const signString = `api_key=${apiKey}&timestamp=${timestamp}`;
const signature = CryptoJS.HmacSHA256(signString, apiSecret).toString();

// 建立WebSocket连接
const ws = new WebSocket("wss://ziario.com/ws");

ws.onopen = function() {
    // 发送认证消息
    ws.send(JSON.stringify({
        action: "auth",
        api_key: apiKey,
        signature: signature,
        timestamp: timestamp
    }));
};

ws.onmessage = function(event) {
    const data = JSON.parse(event.data);
    
    if (data.type === "auth" && data.status === "success") {
        // 认证成功,订阅交易对
        ws.send(JSON.stringify({
            action: "subscribe",
            symbols: ["BTCUSDT", "ETHUSDT"]
        }));
    } else if (data.type === "ticker") {
        // 接收实时行情数据
        console.log("实时价格:", data.price);
        console.log("24小时涨跌幅:", data.change_24h);
    }
};

ws.onerror = function(error) {
    console.error("WebSocket错误:", error);
};

// Python WebSocket示例
import asyncio
import websockets
import json
import hmac
import hashlib
import time

api_key = "your_api_key"
api_secret = "your_api_secret"
timestamp = int(time.time())

# 生成签名
sign_string = f"api_key={api_key}&timestamp={timestamp}"
signature = hmac.new(
    api_secret.encode(),
    sign_string.encode(),
    hashlib.sha256
).hexdigest()

async def connect():
    uri = "wss://ziario.com/ws"
    async with websockets.connect(uri) as websocket:
        # 发送认证消息
        auth_msg = {
            "action": "auth",
            "api_key": api_key,
            "signature": signature,
            "timestamp": timestamp
        }
        await websocket.send(json.dumps(auth_msg))
        
        # 接收认证响应
        response = await websocket.recv()
        data = json.loads(response)
        
        if data.get("status") == "success":
            # 订阅交易对
            subscribe_msg = {
                "action": "subscribe",
                "symbols": ["BTCUSDT", "ETHUSDT"]
            }
            await websocket.send(json.dumps(subscribe_msg))
            
            # 接收实时数据
            while True:
                message = await websocket.recv()
                ticker_data = json.loads(message)
                print(f"实时价格: {ticker_data.get('price')}")

asyncio.run(connect())

Related Documentation