Commit f36c3822 by ken

试试看吧

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