UNPKG

crypto-nodes

Version:

199 lines (131 loc) 4.34 kB
const WebSocket = require('ws'); var per_second = 0; var okex_obj = { init_ws: function (node, symbols) { console.log('Starting OKEX WS'); var that = this; // this.ws = new WebSocket('wss://real.okcoin.com:10440/websocket/okcoinapi'); this.ws = new WebSocket('wss://real.okex.com:10441/websocket/okexapi'); this.ws.on('open', function() { console.log('OKEX CONNECT'); for(x in symbols) { that.ws.send(JSON.stringify({"event":"addChannel", "channel": 'ok_sub_spot_' + symbols[x] + '_depth' })); } }); this.ws.on('message', function(data) { //console.log('OKEX DATA'); //console.log(data); try { data = JSON.parse(data); per_second++; for(x in data) { var row = data[x]; if(row.channel.substr(0, 12) == 'ok_sub_spot_') { var sym_from = row.channel.substr(12, 3).toUpperCase(); var sym_to = row.channel.substr(16, 3).toUpperCase(); console.log({ exchange: 'okex', fees: that.fees, sym_from: sym_from, sym_to: sym_to, data: row }); node.send({ payload: { exchange: 'okex', fees: that.fees, sym_from: sym_from, sym_to: sym_to, data: row }}); } } } catch(e) { console.log(e); } }); this.ws.on('error', function(e) { console.log('OKEX socket error ' + e); }); this.ws.on('close', function() { console.log('OKEX socket close'); setTimeout(function () { that.init_ws(node, symbols); }, 500); }); }, fees: {}, interval: false, connected: false, exchange_symbols: [], configured_symbols: [], enabled_symbols: [], get_exchange_symbols: function(callback) { callback(['btc_usd', 'ltc_usd', 'eth_usd', 'etc_usd', 'bcc_usd' ]); }, get_enabled_symbols: function() { var out = []; for(x in this.exchange_symbols) { var pass = false; var exc_tmp = this.exchange_symbols[x].toUpperCase().split('_'); for(y in this.configured_symbols) { var cfg_tmp = this.configured_symbols[y].split('/'); // If its matching or inverted variant matches its enabled symbol if((exc_tmp[0] == cfg_tmp[0] && exc_tmp[1] == cfg_tmp[1]) || (exc_tmp[0] == cfg_tmp[1] && exc_tmp[1] == cfg_tmp[0]) ) { pass = true; } } if(pass) { out.push(this.exchange_symbols[x]); } } return out; }, init: function(config, node, symbols, callback) { var that = this; this.configured_symbols = symbols; // console.log(symbols); try { this.fees = JSON.parse(config.fees); } catch (e) {} if (!this.interval) { this.interval = setInterval(function() { var color = per_second > 0 ? 'green' : 'red'; node.status({ fill: color, shape: 'ring', text: per_second + ' requests per second' }); per_second = 0; }, 1000); } this.get_exchange_symbols(function (exchange_symbols) { that.exchange_symbols = exchange_symbols; var enabled_symbols = that.get_enabled_symbols(); // that.init_ws(node, enabled_symbols); }); }, } module.exports = function(RED) { function okexConnector(config) { RED.nodes.createNode(this,config); var node = this; node.on('input', function(msg) { if(config.disabled) { // Disable UI Updates if(okex_obj.interval) { clearInterval(okex_obj.interval); } // Set node status node.status({ fill: 'red',shape:'ring', text: 'Disabled' }); // Emit flush orderbook node.send({payload: { exchange: 'hitbtc', op: 'flush' }}); return; } if(msg.payload && msg.payload.op == 'subscribe') { okex_obj.init(config, node, msg.payload.symbols); } }); node.on('close', function() { // tidy up any async code here - shutdown connections and so on. node.status({ fill: 'red',shape:'ring', text: 'Offline' }); node.send({payload: { exchange: 'okex', op: 'flush' }}); }); } RED.nodes.registerType("okexConnector", okexConnector); }