Commit a4357581 by rongjun

bug

parent e58564bc
......@@ -7,7 +7,8 @@ const request = require('request');
const host = 'https://api.kucoin.com';
const CryptoJS = require('crypto-js');
const constants = require('./constants');
const {IPs, mergeDepth} = require('./util');
let totalOrderbook = {};
class biboxApi {
constructor() {
......@@ -30,12 +31,6 @@ class biboxApi {
_doSubscribe(symbols,token, callback) {
const channels = [];
const tickers = [];
for (let item of symbols) {
channels.push(JSON.stringify({event: "addChannel", "channel": `bibox_sub_spot_${item}_depth`, "binary": 1}));
// tickers.push(JSON.stringify({event:"addChannel","channel":`bibox_sub_spot_${item}_ticker`,"binary":0}))
}
const reqURL = `${wsUrl}?bulletToken=${token}&format=json&resource=api`;
const wss = new WebSocket(reqURL, {agent});
wss.on('open', () => {
......@@ -69,6 +64,119 @@ class biboxApi {
})
}
subscribeSymbols(symbols, depth, callback) {
this._publicRequest('/v1/bullet/usercenter/loginUser',{protocol:'websocket',encrypt:true},(error,result)=>{
if(error){
console.log("fetch ws token error");
this.subscribeSymbols(symbols,callback);
}else{
const token = result.data.bulletToken;
const reqURL = `${wsUrl}?bulletToken=${token}&format=json&resource=api`;
const wss = new WebSocket(reqURL, {agent});
wss.on('open', () => {
console.log("websocket on open");
});
wss.on('message', (data) => {
const response = JSON.parse(data);
setInterval(()=>{
wss.send(JSON.stringify({id,type:'ping'}))
},40000);
if(response.type === 'ack'){
const id = response.id;
for(const symbol of symbols){
this._subscribeSymbol(wss, id, symbol, depth)
}
}else if(response.type === 'subscribe'){
const timeStamp = response.data.time;
const symbol = response.data.topic.replace('/trade/','').replace('_TRADE','');
let asks = totalOrderbook[symbol].asks || [];
let bids = totalOrderbook[symbol].bids || [];
let updateData = [];
let newCount = 0
if(result.data.type === 'SELL'){
const oldD = totalOrderbook[symbol];
let has = false;
for(const a of oldD.asks){
if(response.data.price === a[0]) {
has = true;
if (response.data.action === 'ADD') {
newCount += response.data.count;
}
else if (response.data.action === 'CANCEL') {
newCount -= response.data.count;
} else {
}
break;
}
}
if(!has){
updateData = [[response.data.price, response.data.count]];
}else{
updateData = [[response.data.price, newCount]];
}
asks = mergeDepth(asks, updateData, true);
}else if(result.data.type === 'BUY'){
const oldD = totalOrderbook[symbol];
let has = false;
for(const a of oldD.bids){
if(response.data.price === a[0]) {
has = true;
if (response.data.action === 'ADD') {
newCount += response.data.count;
}
else if (response.data.action === 'CANCEL') {
newCount -= response.data.count;
} else {
}
break;
}
}
if(!has){
updateData = [[response.data.price, response.data.count]];
}else{
updateData = [[response.data.price, newCount]];
}
bids = mergeDepth(bids, updateData, false);
}
if(symbol === 'NEO-BTC'){
console.log(asks) //todo 这里输出日志,与网页对比
}
const ret = {SELL: asks, BUY: bids, timestamp: timeStamp, symbol: symbol}
callback(null,ret);
}else{
console.log(data);
}
});
wss.on('error', (error) => {
console.log("websocket error:");
console.log(error);
})
wss.on('close', () => {
console.log("websocket closed");
setTimeout(() => {
this.subscribeSymbols(symbols, depth, callback);
}, 2000);
})
}
});
}
_subscribeSymbol(wss, id, symbol, depth){
console.log('get symbol...')
const ipAddress = IPs[Math.round(Math.random()*(IPs.length-1))];
this.getOrderbook(symbol,depth,ipAddress,(error,result)=>{
if(error){
console.error("get symbol by rest error:");
console.error(error);
this._subscribeSymbol(wss,id,symbol,depth);
return;
}
const data = result.data;
totalOrderbook[symbol]={asks:data.SELL,bids:data.BUY};
wss.send(JSON.stringify({id,type:'subscribe','topic':`/trade/${symbol}_TRADE`}))
});
}
transform(obj) {
var str = [];
for (var p in obj)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment