Node-js:在websocket重新连接后没有收到事件 [英] Node-js: Not receiving events after websocket reconnect
问题描述
我的node-js应用程序使用bitfinex-api-node npm包建立websocket连接以从Bitfinex加密货币交换中接收数据。
my node-js application uses the bitfinex-api-node npm package to establish a websocket connection to receive data from the Bitfinex crypto-currency exchange.
不幸的是,连接在几个小时后无声地中断,应用程序停止通过websocket接收数据。这似乎是一个已知问题,也许是bitfinex-api-module的一个错误。
Unfortunately, the connections silently breaks after some hours and the app stops receiving data over the websocket. This seems to be a known issue, maybe a bug of the bitfinex-api-module.
现在,我试图通过首先连接websocket并订阅一些蜡烛数据来手动重新连接。然后调用websocket.close()函数以在运行时模拟连接错误。在on close函数中,我设置了另一个超时并尝试创建一个新的BFX()对象并打开()但是.on(open)永远不会被调用。
Now, I'm trying to "manually" reconnect by first connecting the websocket and subscribing to some candle data. Then the websocket.close() function gets called to simulate a connection error at runtime. In the on close function I set another timeout and try to create a new BFX() object and open() it but .on(open) never gets called.
=>我一定做错了。我的逻辑中有错误吗?有没有更好的方法重新连接?
=> I must be doing something wrong. Is there a mistake in my logic? Is there a better way to reconnect?
以下代码有效,只需复制粘贴并运行即可查看。
我非常感谢任何提示或提示。
The following code works, just copy paste it and run to see for yourself. I am very thankful for any hints or tips.
const BFX = require('bitfinex-api-node');
//websocket
const opts = {
manageCandles: true,
transform: true,
API_KEY: 'hidden',
API_SECRET: 'hidden',
ws: {
autoReconnect: true,
seqAudit: true,
packetWDDelay: 10 * 1000
}
};
var websocket = new BFX().ws(2, opts);
websocket.on('open', () => {
//websocket.auth.bind(websocket)
console.log('.on(open) called');
websocket.subscribeCandles('trade:5m:tBTCUSD')
});
websocket.on('close', ()=>{
console.log('.on(close) called');
setTimeout(function() {
websocket = new BFX().ws(2, opts);
websocket.open();
}, 10000);
});
websocket.onCandle({key: 'trade:5m:tBTCUSD'},(candles) => {
console.log(candles[0]);
})
websocket.open();
// this closes the connection after 20 seconds
//after start for debugging purposes:
setTimeout(function() {
websocket.close();
}, 10000);
推荐答案
问题是你没有附加任何听众到一个新的websocket实例,当前一个关闭时:
The problem is that you don't attach any listeners to a new instance of websocket, when a previous one is closed:
websocket.on('close', ()=>{
console.log('.on(close) called');
setTimeout(function() {
// this creates a new instance without listeners
websocket = new BFX().ws(2, opts);
websocket.open();
}, 10000);
});
当您第一次初始化websocket时,添加它们:
While when you initialize the websocket for the first time, you add them:
websocket.on('open', /* code */);
websocket.onCandle(/* code */);
为了解决这个问题,我建议编写一个创建和配置websocket新实例的函数: / p>
To resolve the issue, I suggest writing a function that creates and configures a new instance of websocket:
function createWebsocket() {
const websocket = new BFX().ws(2, opts);
websocket.on('open', /* code */);
websocket.onCandle(/* code */);
websocket.open();
}
并在上调用它('close')
:
websocket.on('close', ()=>{
console.log('.on(close) called');
setTimeout(createWebsocket, 10000);
});
这篇关于Node-js:在websocket重新连接后没有收到事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!