Commit 77a4f8dd by ken

试试看

parent 40ab50b0
const stream = 'wss://api.zb.com:9999/websocket'; const stream = 'wss://api.zb.com:9999/websocket';
// const stream = "wss://kline.zb.com:2443/websocket";
const WebSocket = require('ws'); const WebSocket = require('ws');
const request = require('request'); const request = require('request');
const crypto = require('ezcrypto').Crypto; const crypto = require('ezcrypto').Crypto;
...@@ -47,7 +48,8 @@ const _handleSocketOpen = function(opened_callback) { ...@@ -47,7 +48,8 @@ const _handleSocketOpen = function(opened_callback) {
this.isAlive = true; this.isAlive = true;
console.log("open"); console.log("open");
for (const symbol of observerSymbol) { for (const symbol of observerSymbol) {
const req = {event: 'addChannel', channel: symbol + '_depth'}; // const req = {event: 'addChannel', channel: symbol + '_depth'};
const req = {event: 'addChannel', channel: 'dish_depth_00001_' + symbol + "default"};
this.send(JSON.stringify(req)); this.send(JSON.stringify(req));
} }
const accountReq = { const accountReq = {
...@@ -89,6 +91,7 @@ const _subscribe = function(callback, opened_callback = false) { ...@@ -89,6 +91,7 @@ const _subscribe = function(callback, opened_callback = false) {
function subscribe() { function subscribe() {
_subscribe((data) => { _subscribe((data) => {
console.log(data);
if (data.channel === accountInfoChannel) { if (data.channel === accountInfoChannel) {
const coins = data.data.coins; const coins = data.data.coins;
const balances = {}; const balances = {};
...@@ -103,8 +106,7 @@ function subscribe() { ...@@ -103,8 +106,7 @@ function subscribe() {
const lastEventTime = lastEventTimeMap[symbol] || 0; const lastEventTime = lastEventTimeMap[symbol] || 0;
if (depth.timestamp > lastEventTime) { if (depth.timestamp > lastEventTime) {
lastEventTimeMap[symbol] = depth.timestamp; lastEventTimeMap[symbol] = depth.timestamp;
if (symbol === "BTCUSDT") console.log(symbol + '@' + depth.timestamp * 1000 + ' from websocket');
console.log(symbol + '@' + depth.timestamp * 1000 + ' from websocket');
const publishContent = JSON.stringify({symbol, time: depth.timestamp * 1000}); const publishContent = JSON.stringify({symbol, time: depth.timestamp * 1000});
const depthJson = JSON.stringify({asks: depth.asks.reverse(), bids: depth.bids, symbol}); const depthJson = JSON.stringify({asks: depth.asks.reverse(), bids: depth.bids, symbol});
redisClient.set(key, depthJson, 'EX', 2); redisClient.set(key, depthJson, 'EX', 2);
...@@ -146,8 +148,7 @@ function depthByRest(symbol,callback) { ...@@ -146,8 +148,7 @@ function depthByRest(symbol,callback) {
const lastEventTime = lastEventTimeMap[symbol] || 0; const lastEventTime = lastEventTimeMap[symbol] || 0;
if (data.timestamp > lastEventTime) { if (data.timestamp > lastEventTime) {
lastEventTimeMap[symbol] = data.timestamp; lastEventTimeMap[symbol] = data.timestamp;
if (symbol === "BTCUSDT") console.log(symbol + '@' + data.timestamp * 1000 + ' from rest');
console.log(symbol + '@' + data.timestamp * 1000 + ' from rest');
const publishContent = JSON.stringify({symbol, time: data.timestamp * 1000}); const publishContent = JSON.stringify({symbol, time: data.timestamp * 1000});
const depthJson = JSON.stringify({asks: data.asks.reverse(), bids: data.bids, symbol}); const depthJson = JSON.stringify({asks: data.asks.reverse(), bids: data.bids, symbol});
redisClient.set(key, depthJson, 'EX', 2); redisClient.set(key, depthJson, 'EX', 2);
......
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