-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnetProxy.js
63 lines (60 loc) · 1.59 KB
/
netProxy.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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
const Emitter = require('events')
const debug = require('debug')
class NetClientProxy {
constructor() {
this.ev = new Emitter()
this._log = debug('netProxy:client')
this._initEvents()
}
_initEvents() {
this.ev.on('error', err => this._log('Error :', err))
this.ev.on('connect', () => this._log('connect'))
this.ev.on('connected', socket => this._log('connected:', socket))
this.ev.on('disconnect', () => this._log('disconnected'))
this.ev.on('destroy', () => this._log('destroyed'))
this.ev.on('data', data => this._log('data:', data.toString()))
}
get socket() {
return null
}
connect(ip, port) {
this.ev.emit('connect')
this.ev.emit('connected', `${ip}:${port}`)
}
disconnect() {
this.ev.emit('disconnect')
}
send(data) {
this._log('Send :', data)
}
}
class NetServerProxy {
constructor() {
this.ev = new Emitter()
this._log = debug('netProxy:server')
this._initEvents()
}
_initEvents() {
this.ev.on('error', err => this._log('Error:', err))
this.ev.on('start', () => this._log('start listening'))
this.ev.on('stop', () => this._log('stop listening'))
this.ev.on('socket.connect', socket => this._log('s.connect:', socket))
this.ev.on('socket.disconnect', socket => this._log('s.disconnect:', socket))
this.ev.on('socket.data', (socket, data) =>
this._log('data:', socket, data)
)
}
listen(port) {
this.ev.emit('start')
}
stop() {
this.ev.emit('stop')
}
send(socket, data) {
this._log(`Send ${socket}:`, data)
}
}
module.exports = {
NetClientProxy,
NetServerProxy
}