Files
bch-js/src/socket.js
T

29 lines
710 B
JavaScript
Raw Normal View History

2021-01-02 15:41:12 -08:00
const io = require('socket.io-client')
2020-07-27 21:32:51 -07:00
class Socket {
2021-01-02 15:41:12 -08:00
constructor (config = {}) {
if (typeof config === 'string') {
2020-07-27 21:32:51 -07:00
// TODO remove this check in v2.0
this.socket = io(`${config}`)
} else {
if (config.restURL) {
this.socket = io(`${config.restURL}`)
} else {
2021-01-02 15:41:12 -08:00
const restURL = 'https://rest.bitcoin.com'
2020-07-27 21:32:51 -07:00
this.socket = io(`${restURL}`)
}
if (config.callback) config.callback()
}
}
2021-01-02 15:41:12 -08:00
listen (endpoint, cb) {
2020-07-27 21:32:51 -07:00
this.socket.emit(endpoint)
2021-01-02 15:41:12 -08:00
if (endpoint === 'blocks') this.socket.on('blocks', msg => cb(msg))
else if (endpoint === 'transactions') { this.socket.on('transactions', msg => cb(msg)) }
2020-07-27 21:32:51 -07:00
}
}
module.exports = Socket