1

I'm trying to inflate messages from a third party websocket using javascript, but receiving a pako.js

"invalid stored block lengths" error.

The sample code I'm using to implement the web socket can be found here:

https://github.com/okcoin-okex/API-docs-OKEx.com/tree/master/demo

A full working version of the code can be found implemented here:

https://github.com/Tucsky/SignificantTrades/blob/master/src/exchanges/okex.js (https://aggr.trade)

I've tried using zlib.js instead of Pako.js, but running into more difficult problems with that library. I've tried other alternate solutions found on stackoverflow but they don't seem to do what I need.

This is the code I'm running which shows the error. https://jsfiddle.net/fwkgmvjr/

    let myAPI = new WebSocket('wss://real.okex.com:10442/ws/v3');

    //When the connection is first opened
    myAPI.onopen = event => {

      console.log('Socket opened')
      myAPI.send('{"op": "subscribe", "args":["futures/ticker:ETH-USD-190927"]}')
    }

    myAPI.onmessage = event => {


      if (event.data instanceof String) {
        console.log(event.data)
      } else {

        try {
          console.log(JSON.parse(pako.inflateRaw(event.data, {
            to: 'string'
          })))
        } catch (err) {
          console.log(err)
        }
      }
    }

    myAPI.onclose = event => {

      console.log('Socket closed')
    }

    myAPI.onerror = event => {

      console.log(`[error] ${error.message}`)
    }

I expect the message from the websocket to be easily inflatable somehow.

Dvyn Resh
  • 980
  • 1
  • 6
  • 14
Michael P
  • 223
  • 1
  • 9

1 Answers1

4

The solution was to add

  this.api.binaryType = 'arraybuffer'

when declaring the websocket.

Michael P
  • 223
  • 1
  • 9