diff --git a/index.js b/index.js index 23d3125..98f8aab 100644 --- a/index.js +++ b/index.js @@ -1,7 +1,7 @@ const Bcoin = require('./lib/node'); const config = require('./config'); const logger = require('./lib/logger'); -const Api = require('./lib/api'); +const Api = require('./lib/api').server; const db = require('./lib/db'); logger.log('debug', diff --git a/lib/api/index.js b/lib/api/index.js index 44a1e8f..51f9ca6 100644 --- a/lib/api/index.js +++ b/lib/api/index.js @@ -38,4 +38,7 @@ const io = require('socket.io')(server); const SocketAPI = require('./socket')(io); -module.exports = server; +module.exports = { + server, + io, +}; diff --git a/lib/node/index.js b/lib/node/index.js index 42e400a..96851f3 100644 --- a/lib/node/index.js +++ b/lib/node/index.js @@ -4,9 +4,16 @@ const BlockParser = require('../parser').Block; const TxParser = require('../parser').Transaction; const addrParser = require('../parser').Address; const config = require('../../config'); +const io = require('../api').io; const node = new FullNode(config.bcoin); -let socket; + +// Hacky move this to config +let refreshBlocks = false; +// Super Hacky but better than inline Maths. +setInterval(() => { + refreshBlocks = true; +}, 5000); // Only refresh sockets after 5s passes function start() { @@ -25,7 +32,12 @@ function start() { TxParser.parse(entry, block.txs); addrParser.parse(entry, block.txs); - + if (refreshBlocks) { + refreshBlocks = false; + io.sockets.emit('block', { + hash: block.toJSON().hash, + }); + } }); node.pool.on('peer', (peer) => { @@ -49,5 +61,4 @@ function setSocket(client) { module.exports = { start, - setSocket, };