forked from RangerMauve/hyperswarm-proxy-ws
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
33 lines (28 loc) · 821 Bytes
/
server.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
const DSwarmProxyServer = require('@dswarm/proxy/server')
const websocket = require('websocket-stream')
const http = require('http')
class DSwarmProxyWSServer extends DSwarmProxyServer {
constructor (opts = {}) {
super(opts)
const { server } = opts
if (server) this.listenOnServer(server)
}
listenOnServer (server) {
this.server = server
this.websocketServer = websocket.createServer({ server }, (socket) => {
this.handleStream(socket)
})
}
listen (...args) {
const server = http.createServer()
this.listenOnServer(server)
server.listen(...args)
}
destroy (cb) {
// Closing the server rather than the websocket server actually closes the handles. 🤯
this.server.close(() => {
super.destroy(cb)
})
}
}
module.exports = DSwarmProxyWSServer