-
Notifications
You must be signed in to change notification settings - Fork 0
/
connect.js
36 lines (29 loc) · 897 Bytes
/
connect.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
34
35
36
const net = require('net')
const shortid = require('shortid')
const Module = require('@microverse-network/core/module')
const makeArrayTransformer = require('./arraytransformer')
const makeBufferTransformer = require('./buffertransformer')
module.exports = class TCPSocketProxy extends Module {
handleReady() {
super.handleReady()
this.createServer()
}
createServer() {
this.server = new net.Server()
this.server.listen(this.options.port || 0, () => {
this.emit('listening')
})
this.server.on('connection', socket => {
const stream = this.mux.createStream(shortid.generate())
this.bindSocketStream(socket, stream)
})
}
handleMux(mux) {
super.handleMux(mux)
this.mux = mux
}
bindSocketStream(socket, stream) {
socket.pipe(makeArrayTransformer()).pipe(stream)
stream.pipe(makeBufferTransformer()).pipe(socket)
}
}