嗨,我正在尝试使用 binance api 实现本地订单簿,但是我时不时地不断收到此错误,但它并不总是发生,但如果它确实会在早期发生,请帮助
这是运行从外部文件调用导出函数以处理来自消息的 Web 套接字的数据的主文件
wsClient.subscribeSpotDiffBookDepth("btcusdt");
wsClient.on('message', (data) => {
// Setup and process order book information
order_book.update_orderbook(data);
if(order_book.ready === 1){
order_book.get_orderbook_history();
}
});
wsClient.on('error', err => {
/* handle error */
console.log("this is it 2 " + err);
});
wsClient.on('close', () => {
/* ... */
});
外部文件中的导出函数
exports.update_orderbook = function(data) {
if(data.e == "depthUpdate"){
if(this.ready === 0){
this.ready = 1;
console.log("Stage 1 in play");
}else{
this.buffer += data;
}
if(this.ready === 2 && this.asks != null && this.bids != null){
if(undefined !== this.asks && undefined !== this.bids && undefined !== data.a && undefined !== data.b){
if(data.U <= this.lastUpdateId + 1 && data.u >= this.lastUpdateId + 1){
// error is coming from calling this function-------------------------------------------------------
var temp_array1 = sort_array(this.asks, data.a);
var temp_array2 = sort_array(this.bids, data.b);
this.asks = temp_array1;
this.bids = temp_array2;
this.lastUpdateId = data.u;
console.log("Stage 3");
}
}
}else{
this.buffer += data;
}
}
}
排序和更新数组的函数
function sort_array(array1, array2){
for(let x in array2){
if(array2[x][1] == 0){
for(let i in array2){
if(array1[i][0] === array2[x][0]){
array1.splice(i, 1);
}
}
}else{
var in_array = false;
for(let i in array1){
// this seems to be the problem area---------------------------------------------------------
if(array1[i][0] === array2[x][0]){
array1[i] = array2[x];
in_array = true;
}
}
if(!in_array){
array1.push(array2[x]);
}
}
}
return array1;
}
错误日志