81 lines
1.8 KiB
JavaScript
Raw Normal View History

2016-03-14 20:25:00 +00:00
// const debug = require('debug')
// const log = debug('libp2p:tcp')
// const multiaddr = require('multiaddr')
const SWS = require('simple-websocket')
2016-02-26 13:18:05 +00:00
2016-03-14 20:25:00 +00:00
exports = module.exports = WebSockets
2016-02-26 13:18:05 +00:00
2016-03-14 20:25:00 +00:00
function WebSockets () {
if (!(this instanceof WebSockets)) {
return new WebSockets()
}
const listeners = []
this.dial = function (multiaddr, options) {
if (!options) {
options = {}
}
options.ready = options.ready || function noop () {}
const maOpts = multiaddr.toOptions()
const conn = new SWS('ws://' + maOpts.host + ':' + maOpts.port)
conn.on('ready', options.ready)
conn.getObservedAddrs = () => {
return [multiaddr]
}
return conn
}
this.createListener = (multiaddrs, options, handler, callback) => {
if (typeof options === 'function') {
callback = handler
handler = options
options = {}
}
if (!Array.isArray(multiaddrs)) {
multiaddrs = [multiaddrs]
}
2016-02-26 13:18:05 +00:00
2016-03-14 20:25:00 +00:00
var count = 0
multiaddrs.forEach((m) => {
const listener = SWS.createServer((conn) => {
conn.getObservedAddrs = () => {
return [] // TODO think if it makes sense for WebSockets
}
handler(conn)
})
listener.listen(m.toOptions().port, () => {
if (++count === multiaddrs.length) {
callback()
}
})
listeners.push(listener)
})
}
this.close = (callback) => {
if (listeners.length === 0) {
throw new Error('there are no listeners')
}
var count = 0
listeners.forEach((listener) => {
listener.close(() => {
if (++count === listeners.length) {
callback()
}
})
})
}
this.filter = (multiaddrs) => {
return multiaddrs.filter((ma) => {
// TODO
// https://github.com/whyrusleeping/js-mafmt/pull/2
})
}
}