diff options
| author | Adam Soltys <[email protected]> | 2019-11-05 22:09:50 -0800 |
|---|---|---|
| committer | Adam Soltys <[email protected]> | 2019-11-05 22:09:50 -0800 |
| commit | ae5e489e8a21293ae3f4d9a3e1940a62ddd01011 (patch) | |
| tree | 6935013d4cb90f4830f236697905db2a6c123dc8 | |
| parent | f59d942805d9809ee7036a45c9178bf32eff07eb (diff) | |
| download | coinos-server-ae5e489e8a21293ae3f4d9a3e1940a62ddd01011.tar.gz coinos-server-ae5e489e8a21293ae3f4d9a3e1940a62ddd01011.zip | |
cache fx rates and periodically re-establish websocket
| -rw-r--r-- | binance.js | 7 | ||||
| -rw-r--r-- | index.js | 3 | ||||
| -rw-r--r-- | rates.js | 17 |
3 files changed, 19 insertions, 8 deletions
@@ -1,5 +1,4 @@ const WebSocket = require("ws"); - module.exports = (app, socket) => { const binance = new WebSocket( "wss://stream.binance.com:9443/ws/btcusdt@ticker" @@ -11,7 +10,7 @@ module.exports = (app, socket) => { app.set("ask", msg.a); app.set("last", msg.l); - let fx = app.get("rates"); + let fx = app.get("fxrates"); if (!fx) return; let rates = {}; @@ -19,6 +18,8 @@ module.exports = (app, socket) => { rates[symbol] = msg.a * fx[symbol]; }); - socket.emit("rates", rates); + app.set("rates", rates); }; + + return binance; }; @@ -39,8 +39,7 @@ const l = console.log; }) ); - require("./binance")(app, socket); - require("./rates")(app); + require("./rates")(app, socket); const seen = []; const addresses = {}; @@ -2,7 +2,9 @@ const axios = require("axios"); const config = require("./config"); const l = console.log; -module.exports = app => { +module.exports = (app, socket) => { + let binance = require("./binance")(app, socket); + let fetchRates; (fetchRates = async () => { try { @@ -26,11 +28,20 @@ module.exports = app => { rates.VES = res.data["exchange_rates"].VEF_USD / 100000; rates.KVES = res.data["exchange_rates"].VEF_USD / 100000000; - app.set("rates", rates); + app.set("fxrates", rates); } catch (e) { l(e); } - setTimeout(fetchRates, 3600000); + setTimeout(fetchRates, 7200000); })(); + + setInterval(() => { + socket.emit("rates", app.get("rates")); + }, 1000); + + setInterval(() => { + binance.terminate(); + binance = require("./binance")(app, socket); + }, 360000); }; |
