Skip to content

Commit

Permalink
chore: small changes
Browse files Browse the repository at this point in the history
  • Loading branch information
surunzi committed Sep 20, 2024
1 parent bdad810 commit 27fadf1
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 13 deletions.
15 changes: 13 additions & 2 deletions server/lib/ChannelManager.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const Emitter = require('licia/Emitter');
const truncate = require('licia/truncate');
const ansiColor = require('licia/ansiColor');
const util = require('./util');
const Channel = require('licia/Channel');

module.exports = class ChannelManager extends Emitter {
constructor() {
Expand All @@ -11,7 +12,7 @@ module.exports = class ChannelManager extends Emitter {
this._clients = {};
}
createTarget(id, ws, url, title, favicon, ip, userAgent) {
const channel = util.createChannel(ws);
const channel = createChannel(ws);

util.log(`${ansiColor.yellow('target')} ${id}:${truncate(title, 10)} ${ansiColor.green('connected')}`);
this._targets[id] = {
Expand Down Expand Up @@ -39,7 +40,7 @@ module.exports = class ChannelManager extends Emitter {
return ws.close();
}

const channel = util.createChannel(ws);
const channel = createChannel(ws);
util.log(
`${ansiColor.blue('client')} ${id} ${ansiColor.green('connected')} to target ${target.id}:${truncate(
target.title,
Expand Down Expand Up @@ -79,3 +80,13 @@ module.exports = class ChannelManager extends Emitter {
return this._clients;
}
};

function createChannel(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;
}
11 changes: 0 additions & 11 deletions server/lib/util.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
const dateFormat = require('licia/dateFormat');
const toArr = require('licia/toArr');
const Channel = require('licia/Channel');

exports.log = function () {
const args = toArr(arguments);
Expand All @@ -9,13 +8,3 @@ 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 27fadf1

Please sign in to comment.