Commit 3d617f8e by ken

Initial commit

parents
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
# Runtime data
pids
*.pid
*.seed
*.pid.lock
# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
# Coverage directory used by tools like istanbul
coverage
# nyc test coverage
.nyc_output
# Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files)
.grunt
# Bower dependency directory (https://bower.io/)
bower_components
# node-waf configuration
.lock-wscript
# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release
# Dependency directories
node_modules/
jspm_packages/
# Typescript v1 declaration files
typings/
# Optional npm cache directory
.npm
# Optional eslint cache
.eslintcache
# Optional REPL history
.node_repl_history
# Output of 'npm pack'
*.tgz
# Yarn Integrity file
.yarn-integrity
# dotenv environment variables file
.env
# next.js build output
.next
.idea/
package-lock.json
\ No newline at end of file
{
"name": "zbnode",
"version": "1.0.0",
"description": "",
"main": "zb.js",
"scripts": {
"zb": "node $NODE_DEBUG_OPTION zb.js"
},
"author": "",
"license": "ISC",
"dependencies": {
"redis": "^2.8.0",
"ws": "^5.1.0"
}
}
const stream = 'wss://api.zb.com:9999/websocket';
const WebSocket = require('ws');
const options = {};
const observerSymbol = ["btcusdt"];
const redis = require('redis');
const redisClient = redis.createClient();
const publishKey = "ZB@2";
const _handleSocketError = function(error) {
// Errors ultimately result in a `close` event.
// see: https://github.com/websockets/ws/blob/828194044bf247af852b31c49e2800d557fedeff/lib/websocket.js#L126
console.log('WebSocket error: '+this.endpoint+
(error.code ? ' ('+error.code+')' : '')+
(error.message ? ' '+error.message : '')
);
};
const _handleSocketOpen = function(opened_callback) {
this.isAlive = true;
console.log("open");
for (const symbol of observerSymbol) {
const req = {event: 'addChannel', channel: symbol + '_depth'};
this.send(JSON.stringify(req));
}
};
const _handleSocketClose = function(code, reason) {
console.log(code);
console.log(reason);
console.log('close');
subscribe();
};
const _subscribe = function(callback, opened_callback = false) {
const ws = new WebSocket(stream);
ws.reconnect = options.reconnect;
ws.isAlive = false;
ws.on('open', _handleSocketOpen.bind(ws, opened_callback));
// ws.on('pong', _handleSocketHeartbeat);
ws.on('error', _handleSocketError);
ws.on('close', _handleSocketClose.bind(ws));
ws.on('message', function(data) {
try {
callback(JSON.parse(data));
} catch (error) {
console.log('Parse error: '+error.message);
}
});
return ws;
};
function subscribe() {
_subscribe((depth) => {
console.log(depth);
const key = depth.channel.replace('_', '').toUpperCase() + '@zb';
const symbol = depth.channel.replace('_depth', '').toUpperCase();
const publishContent = JSON.stringify({symbol, time: depth.timestamp * 1000})
const depthJson = JSON.stringify({asks: depth.asks.reverse(), bids: depth.bids, symbol})
redisClient.set(depthJson, key);
redisClient.publish(publishKey, publishContent);
})
}
subscribe();
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