Bitfinex API网络套接字订单

时间:2019-04-27 01:18:25

标签: javascript api websocket

我正在尝试通过bitfacex bitfex websocks获取订单,但是在控制台中出现错误。 控制台中的错误:如果(bidsKeys [i]){

,则无法读取未定义的属性'0'
var ws = new WebSocket("wss://api-pub.bitfinex.com/ws/2");


ws.onopen = function() {
    // ws.send(JSON.stringify({ event: 'conf', flags: 131072 }))
    let msg = JSON.stringify({
        event: 'subscribe',
        channel: 'book',
        pair: 'tBTCUSD',
        freq: 'F1',
        len: '25',
        prec: 'P0'
    })
    ws.send(msg)


    ws.onmessage = function(msg) {
        const BOOK = {}
        BOOK.bids = {}
        BOOK.asks = {}
        BOOK.psnap = {}
        BOOK.mcnt = 0
        msg = JSON.parse(msg.data)
        //  var response = JSON.parse(msg.data);   
        //  console.log(msg.data);
        const csdata = []
        const bidsKeys = BOOK.psnap['bids']
        const asksKeys = BOOK.psnap['asks']

        for (let i = 0; i < 25; i++) {
            if (bidsKeys[i]) {
                const price = bidsKeys[i]
                const pp = BOOK.bids[price]
                csdata.push(pp.price, pp.amount)
            }
            if (asksKeys[i]) {
                const price = asksKeys[i]
                const pp = BOOK.asks[price]
                csdata.push(pp.price, -pp.amount)
            }
        }

        const csStr = csdata.join(':')
        const csCalc = CRC.str(csStr)

        if (csCalc !== checksum) {
            console.error('CHECKSUM_FAILED')
        }
    }

0 个答案:

没有答案