I receive these huge Strings via WebSocket:
[
"BTC-31DEC21-100000-P",
"{\"data\":{\"bids\":{\"0.01\":{\"price\":0.01,\"volume\":66.2,\"exchange\":\"DER\"},\"5.0E-4\":{\"price\":5.0E-4,\"volume\":1.1,\"exchange\":\"DER\"},\"0.637\":{\"price\":0.637,\"volume\":8.4,\"exchange\":\"DER\"}},\"asks\":{\"0.664\":{\"price\":0.664,\"volume\":8.4,\"exchange\":\"DER\"}}},\"isMasterFrame\":true}"
]
or
[
"BTC-31DEC21-36000-C",
"{\"data\":[{\"price\":0.422,\"volume\":8.4,\"exchange\":\"DER\",\"side\":\"ASKS\"},{\"price\":0.423,\"volume\":0.0,\"exchange\":\"DER\",\"side\":\"ASKS\"}],\"isMasterFrame\":false}"
]
or
[
"BTC-31DEC21-60000-P",
"{\"data\":[{\"price\":0.105,\"volume\":0.0,\"exchange\":\"DER\",\"side\":\"ASKS\"},{\"price\":0.1055,\"volume\":28.7,\"exchange\":\"DER\",\"side\":\"ASKS\"},{\"price\":0.106,\"volume\":7.6,\"exchange\":\"DER\",\"side\":\"ASKS\"},{\"price\":0.1065,\"volume\":43.0,\"exchange\":\"DER\",\"side\":\"ASKS\"}],\"isMasterFrame\":false}"
]
I want to make JavaScript Buffer where I can store the data and reset it if I receive JSON with isMasterFrame = true
let payload = JSON.parse(messageString[1]);
if (payload.hasOwnProperty("isMasterFrame")) {
for (let i = 0; i < payload.pairs.length; i++) {
let currentPair = payload.data[i]
currentPair = currentPair.replace(/\0/g, ''); //Remove null chars
if (currentPair.toUpperCase() != 'KILL') {
// reset the buffer and start again to fill data
}
}
} else {
// if we receive "isMasterFrame":false just update the data without reset
}
What are the available options to implement this buffer?
if(payload.data.isMasterFrame) { ... }, and where is thepairsproperty in the data (did I miss it)? It also appears the reset is conditional upon thecurrentPairnot being equal toKILL. Is that true?let pairsCache = [];, then.push()onto the array if not a masterframe and not KILL otherwise set the cache array back topairsCache = [];