我的 Nodejs 程序的最终目标是订阅多个频道,提供实时金融信息并处理流数据。
在 python 中,可以使用 functools/partial 和 websocket 订阅多个频道。这允许一个 python 程序订阅多个频道。就我而言,我正在尝试使用 nodejs/javascript 做同样的事情。
Python websockets,订阅多个频道 https://stackoverflow.com/questions/45645865/python-websockets-subscribe-to-multiple-channels
是否可以结合 wesocket 和其他一些包在 nodejs 中执行类似的操作?
我下面的内容有效,但它在一个输出中输出来自三个不同通道的流,这很难管理。
/*jshint esversion: 6 */
var sys = require('util');
const WebSocket = require('ws')
const wss = new WebSocket('wss://api.bitfinex.com/ws/')
wss.onopen = () => {
console.log('Subscribing: BTCUSD');
wss.send(JSON.stringify(
{
"event":"subscribe",
"channel":"book",
"pair":"BTCUSD",
"prec":"P0",
"freq":"F1"
}
));
console.log('Subscribing: LTCUSD');
wss.send(JSON.stringify(
{
"event":"subscribe",
"channel":"book",
"pair":"LTCUSD",
"prec":"P0",
"freq":"F1"
}
));
console.log('Subscribing: LTCBTC');
wss.send(JSON.stringify(
{
"event":"subscribe",
"channel":"book",
"pair":"LTCBTC",
"prec":"P0",
"freq":"F1"
}
));
console.log('Subscribing: ETHUSD');
wss.send(JSON.stringify(
{
"event":"subscribe",
"channel":"book",
"pair":"ETHUSD",
"prec":"P0",
"freq":"F1"
}
));
}
wss.onmessage = (msg) => {
var response = JSON.parse(msg.data);
var head = response["event"];
var head_body = response[1];
if ( head == "subscribed" ) {
console.log("channelID = ", response["chanId"], " currency = ", response["pair"]);
} else {
console.log(msg.data);
}
}
感谢对此的指导。我也在看socket.io。或许这样可以做到吗?
None
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)