Commit f36c3822 by ken

试试看吧

parent f39fbdc5
......@@ -12,7 +12,7 @@ setInterval(()=>{
function run() {
const before = Date.now();
depthByRest((err, data) => {
depthByRest("btcusdt",(err, data) => {
if (err) {
console.error(err);
return;
......
......@@ -10,6 +10,8 @@ const publishKey = "ZB@2";
const accountInfoChannel = "getaccountinfo";
const APIKEY = "c2e5bd19-6ac3-489e-8e3a-b6e40b4d5b6d";
const APISECRET = "15f974c4-fc64-462e-a815-cd0469156300";
const lastEventTimeMap = {};
const _handleSocketError = function(error) {
// Errors ultimately result in a `close` event.
......@@ -72,19 +74,23 @@ function subscribe() {
const depth = data;
const key = depth.channel.replace('_', '').toUpperCase() + '@zb';
const symbol = depth.channel.replace('_depth', '').toUpperCase();
console.log(symbol + '@' + depth.timestamp * 1000 + ' latency=' + (Date.now() - depth.timestamp * 1000) + 'ms');
const publishContent = JSON.stringify({symbol, time: depth.timestamp * 1000})
const depthJson = JSON.stringify({asks: depth.asks.reverse(), bids: depth.bids, symbol})
const lastEventTime = lastEventTimeMap[symbol] || 0;
if (depth.timestamp > lastEventTime) {
lastEventTimeMap[symbol] = depth.timeStamp;
console.log(symbol + '@' + depth.timestamp * 1000 + ' from websocket');
const publishContent = JSON.stringify({symbol, time: depth.timestamp * 1000});
const depthJson = JSON.stringify({asks: depth.asks.reverse(), bids: depth.bids, symbol});
redisClient.set(key, depthJson, 'EX', 1);
redisClient.publish(publishKey, publishContent);
}
}
});
}
function depthByRest(callback) {
function _request(method, url, callback) {
let opt = {
url: "http://api.zb.com/data/v1/depth?market=btc_usdt&size=10",
method: "GET",
url,
method,
headers: {
Connection: "Keep-Alive"
}
......@@ -102,6 +108,38 @@ function depthByRest(callback) {
});
}
subscribe();
function depthByRest(symbol,callback) {
const url = `http://api.zb.com/data/v1/depth?market=${symbol}&size=10`;
_request("GET", url, (err, data) => {
if (err) {
console.error(err);
} else {
const key = symbol.replace('_', '').toUpperCase() + '@zb';
const lastEventTime = lastEventTimeMap[symbol] || 0;
if (data.timestamp > lastEventTime) {
lastEventTimeMap[symbol] = data.timeStamp;
console.log(symbol + '@' + data.timestamp * 1000 + ' from rest');
const publishContent = JSON.stringify({symbol, time: data.timestamp * 1000});
const depthJson = JSON.stringify({asks: data.asks.reverse(), bids: data.bids, symbol});
redisClient.set(key, depthJson, 'EX', 1);
redisClient.publish(publishKey, publishContent);
}
}
if (callback) {
callback(err, data);
}
});
}
function run() {
subscribe();
setInterval(() => {
for (const symbol of observerSymbol) {
depthByRest(symbol);
}
}, 500);
}
run();
module.exports = depthByRest
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