Skip to content

Commit

Permalink
just in case ws is slow
Browse files Browse the repository at this point in the history
  • Loading branch information
abanchev committed Aug 19, 2021
1 parent 529ba92 commit aa5a867
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 6 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"express": "^4.17.1",
"http-proxy-middleware": "^2.0.1",
"lodash": "^4.17.21",
"node-binance-api": "^0.12.5"
"node-binance-api": "^0.12.5",
"node-fetch": "^2.6.1"
}
}
12 changes: 8 additions & 4 deletions src/client.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Binance from 'node-binance-api';
import fetch from 'node-fetch';

class Client {
client;
Expand Down Expand Up @@ -46,12 +47,15 @@ class Client {
let data = this.klines[symbol + interval];
if (!data) {
this.subscribe(symbol, interval);
while (!data || data.length === 0) {
await new Promise(r => setTimeout(r, 100));
data = this.klines[symbol + interval];
}
}
while (!data || data.length === 0 || !data[data.length - 1] || data[data.length - 1][6] <= Date.now()) {
await new Promise(r => setTimeout(r, 20));
data = this.klines[symbol + interval];
if (!data[data.length - 1] || data[data.length - 1][6] <= Date.now()) {
return fetch(`https://api.binance.com/api/v3/klines?symbol=${symbol}&interval=${interval}`).then(resp => resp.json());
}
return data;
return this.klines[symbol + interval];
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const getServer = async (client) => {
app.use(express.json());

router.get('/api/v3/klines', async (req, res) => {
res.send(await client.getCandles(req.query.symbol, req.query.interval));
client.getCandles(req.query.symbol, req.query.interval).then(result => res.send(result))
});

router.use(createProxyMiddleware({
Expand Down

0 comments on commit aa5a867

Please sign in to comment.