Adding websocket.

This commit is contained in:
MiguelMedeiros\Miguel Medeiros
2021-02-09 16:22:48 -03:00
parent b39fb0bd60
commit 90be85f6f0
6 changed files with 121 additions and 30 deletions

View File

@@ -3,12 +3,14 @@ import mempool from './mempool';
import blocks from './blocks';
import transactions from './transactions';
import addresses from './addresses';
import websocket from './websocket';
export { default as fees } from './fees';
export { default as mempool } from './mempool';
export { default as blocks } from './blocks';
export { default as transactions } from './transactions';
export { default as addresses } from './addresses';
export { default as websocket } from './websocket';
export default {
fees,
@@ -16,4 +18,5 @@ export default {
blocks,
transactions,
addresses,
websocket,
};

22
src/websocket.ts Normal file
View File

@@ -0,0 +1,22 @@
const WebSocket = require('ws');
const ws = new WebSocket('wss://mempool.space/api/v1/ws');
const init = (params: { options: string[] }) => {
ws.on('open', function open() {
ws.send(JSON.stringify({ action: 'init' }));
setInterval(function timeout() {
ws.send(
JSON.stringify({
action: 'want',
data: params.options,
})
);
}, 500);
});
return ws;
};
export default {
init,
};