2016-09-04 59 views
0

我想缩小此nodejs脚本的实时输出。nodejs - 过滤器实时输出

var autobahn = require('autobahn'); 
var wsuri = "wss://api.poloniex.com"; 
var connection = new autobahn.Connection({ 
    url: wsuri, 
    realm: "realm1" 
}); 

connection.onopen = function (session) { 
     function marketEvent (args,kwargs) { 
       console.log(args); 
     } 
     function tickerEvent (args,kwargs) { 
       console.log(args); 
     } 
     function trollboxEvent (args,kwargs) { 
       console.log(args); 
     } 
     session.subscribe('BTC_XMR', marketEvent); 
     session.subscribe('ticker', tickerEvent); 
     session.subscribe('trollbox', trollboxEvent); 
} 

connection.onclose = function() { 
    console.log("Websocket connection closed"); 
} 

connection.open(); 

剧本是从交换的API文档采取: https://poloniex.com/support/api/

当我在节点运行它,我得到的所有货币对的实时更新。如何仅输出选定的一对?

我在文档中看不到类似的东西。

+0

你想要什么样的配对? – theonlygusti

+0

我想要BTC_SDC。谢谢。 – Wasteland

+0

将session.subscribe('BTC_XMR',marketEvent);'改为'session.subscribe('BTC_SDC',marketEvent);'?我目前无法访问节点环境,因此不知道输出结果如何:P – theonlygusti

回答

1
function tickerEvent (args,kwargs) { 
     if(args[0] !== "BTC_SDC"){ 
      return; // causes anything not "BTC_SDC" related to be filtered out. 
     } 
     console.log(args); // information you want. 
    } 

我建议在ticker中该数组的第一个项目的外壳。

+0

辉煌 - 就是这样。你是什​​么意思'套在第一个项目上'? – Wasteland

+1

使用'switch'开关https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/switch 而不是写其他如果其他如果其他如果反复获得更多的信息和做不同这些“案例”中的事情。该API也发送了一个非常糟糕的形状有效载荷,我建议将它映射到一个对象,以便更容易理解,并且您不使用“幻数”来知道该数组中的事物的顺序。 –