diff --git a/package.json b/package.json index 99721efd..5d59bff9 100644 --- a/package.json +++ b/package.json @@ -70,7 +70,7 @@ "koa-compress": "^4.0.1", "koa-router": "^8.0.8", "koa-send": "^5.0.0", - "licia": "^1.37.0", + "licia": "^1.42.0", "ws": "^7.2.3" } } diff --git a/server/lib/Channel.js b/server/lib/Channel.js deleted file mode 100644 index 5fc15af4..00000000 --- a/server/lib/Channel.js +++ /dev/null @@ -1,63 +0,0 @@ -const Emitter = require('licia/Emitter'); -const each = require('licia/each'); -const some = require('licia/some'); -const remove = require('licia/remove'); - -module.exports = class Channel extends Emitter { - constructor(ws) { - super(); - this._ws = ws; - this._connections = []; - this.send = message => ws.send(message); - - ws.on('close', (...args) => { - this.emit('close', ...args); - this.destroy(); - }); - ws.on('message', (...args) => { - each(this._connections, connection => { - connection.send(...args); - }); - this.emit('message', ...args); - }); - ws.on('error', error => { - this.emit('error', error); - }); - } - send(message) { - this._ws.send(message); - } - destroy() { - each(this._connections, connection => { - connection.off('message', connection); - }); - this._connections = []; - this._ws.close(); - } - isConnected(connection) { - if (this.hasConnection(connection)) return true; - if (connection.hasConnection(this)) return true; - - return false; - } - hasConnection(connection) { - return some(this._connections, item => item === connection); - } - connect(connection) { - if (this.isConnected(connection)) return; - - this._connections.push(connection); - connection.on('message', this.send); - connection.on('close', () => this.disconnect(connection)); - } - disconnect(connection) { - if (!this.isConnected(connection)) return; - - if (this.hasConnection(connection)) { - remove(this._connections, item => item === connection); - connection.off('message', this.send); - } else { - connection.disconnect(this); - } - } -}; diff --git a/server/lib/ChannelManager.js b/server/lib/ChannelManager.js index 1f35e631..b4f4b175 100644 --- a/server/lib/ChannelManager.js +++ b/server/lib/ChannelManager.js @@ -1,4 +1,3 @@ -const Channel = require('./Channel'); const Emitter = require('licia/Emitter'); const truncate = require('licia/truncate'); const ansiColor = require('licia/ansiColor'); @@ -12,7 +11,7 @@ module.exports = class ChannelManager extends Emitter { this._clients = {}; } createTarget(id, ws, url, title, favicon, ip, userAgent) { - const channel = new Channel(ws); + const channel = util.createChannel(ws); util.log(`${ansiColor.yellow('target')} ${id}:${truncate(title, 10)} ${ansiColor.green('connected')}`); this._targets[id] = { @@ -21,13 +20,14 @@ module.exports = class ChannelManager extends Emitter { url, favicon, channel, + ws, ip, userAgent, rtc: ws.rtc, }; - channel.on('close', () => this.removeTarget(id, title)); - channel.on('error', error => { + ws.on('close', () => this.removeTarget(id, title)); + ws.on('error', error => { util.log(`${ansiColor.yellow('target')} ${id}:${truncate(title, 10)} ${ansiColor.red('error')} ${error.message}`); }); @@ -39,7 +39,7 @@ module.exports = class ChannelManager extends Emitter { return ws.close(); } - const channel = new Channel(ws); + const channel = util.createChannel(ws); util.log( `${ansiColor.blue('client')} ${id} ${ansiColor.green('connected')} to target ${target.id}:${truncate( target.title, @@ -51,11 +51,16 @@ module.exports = class ChannelManager extends Emitter { this._clients[id] = { id, target: target.id, + ws, channel, }; - channel.on('close', () => this.removeClient(id)); - target.channel.on('close', () => channel.destroy()); + const closeClientWs = () => ws.close(); + ws.on('close', () => { + target.ws.removeListener('close', closeClientWs); + this.removeClient(id); + }); + target.ws.on('close', closeClientWs); } removeTarget(id, title = '') { util.log(`${ansiColor.yellow('target')} ${id}:${title} ${ansiColor.red('disconnected')}`); diff --git a/server/lib/util.js b/server/lib/util.js index 41f481e4..09efeaef 100644 --- a/server/lib/util.js +++ b/server/lib/util.js @@ -1,5 +1,6 @@ const dateFormat = require('licia/dateFormat'); const toArr = require('licia/toArr'); +const Channel = require('licia/Channel'); exports.log = function () { const args = toArr(arguments); @@ -8,3 +9,13 @@ exports.log = function () { console.log.apply(console, args); }; + +exports.createChannel = function (ws) { + const channel = new Channel(); + + ws.on('close', () => channel.destroy()); + ws.on('message', msg => channel.send(msg)); + channel.on('message', msg => ws.send(msg)); + + return channel; +};