diff options
author | Florrie <towerofnix@gmail.com> | 2020-07-10 11:24:23 -0300 |
---|---|---|
committer | Florrie <towerofnix@gmail.com> | 2020-07-10 11:25:10 -0300 |
commit | 011004f9ac28c3a52c57ffb68ad0f0594095ea56 (patch) | |
tree | 47f180960962999a6e57ef951d7a3da254037550 /telnet.js | |
parent | 3bdaa6dd24e258dbecbc9efe11b7a583246c7b79 (diff) |
rename telnet-server.js to telnet.js
...for consistency with a socket.js (coming soon to a theater near you!)
Diffstat (limited to 'telnet.js')
-rw-r--r-- | telnet.js | 81 |
1 files changed, 81 insertions, 0 deletions
diff --git a/telnet.js b/telnet.js new file mode 100644 index 0000000..33e3dcc --- /dev/null +++ b/telnet.js @@ -0,0 +1,81 @@ +'use strict' + +const EventEmitter = require('events') +const net = require('net') +const setupClient = require('./client') + +const { + util: { + TelnetInterfacer + } +} = require('tui-lib') + +class TelnetServer extends EventEmitter { + constructor(backend) { + super() + + this.backend = backend + this.server = new net.Server(socket => this.handleConnection(socket)) + this.sockets = [] + } + + listen(port) { + this.server.listen(port) + } + + async handleConnection(socket) { + const interfacer = new TelnetInterfacer(socket) + const { appElement, cleanTerminal, flushable } = await setupClient({ + backend: this.backend, + writable: socket, + interfacer, + appConfig: { + canControlPlayback: false, + canControlQueue: true, + canControlQueuePlayers: false, + canProcessMetadata: false, + canSuspend: false, + showTabberPane: true, + stopPlayingUponQuit: false, + menubarColor: 2 + } + }) + + appElement.attachAsServerClient(this) + + let closed = false + + const quit = (msg = 'See you!') => { + cleanTerminal() + interfacer.cleanTelnetOptions() + socket.write('\r' + msg + '\r\n') + socket.end() + flushable.end() + closed = true + this.sockets.splice(this.sockets.indexOf(socket), 1) + } + + appElement.on('quitRequested', quit) + + socket.on('close', () => { + if (!closed) { + flushable.end() + closed = true + this.sockets.splice(this.sockets.indexOf(socket), 1) + } + this.emit('left', socket) + }) + + socket.quit = quit + this.sockets.push(socket) + this.emit('joined', socket) + } + + disconnectAllSockets(msg) { + while (this.sockets.length) { + this.sockets[0].quit(msg) + } + } +} + +module.exports = TelnetServer |