Skip to content

Commit

Permalink
chore: use licia Channel
Browse files Browse the repository at this point in the history
  • Loading branch information
surunzi committed Sep 20, 2024
1 parent 560d30f commit bdad810
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 71 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
}
63 changes: 0 additions & 63 deletions server/lib/Channel.js

This file was deleted.

19 changes: 12 additions & 7 deletions server/lib/ChannelManager.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const Channel = require('./Channel');
const Emitter = require('licia/Emitter');
const truncate = require('licia/truncate');
const ansiColor = require('licia/ansiColor');
Expand All @@ -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] = {
Expand All @@ -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}`);
});

Expand All @@ -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,
Expand All @@ -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')}`);
Expand Down
11 changes: 11 additions & 0 deletions server/lib/util.js
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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;
};

0 comments on commit bdad810

Please sign in to comment.