55 lines
1.8 KiB
JavaScript
55 lines
1.8 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
var logger = require('../logger');
|
|
class ChannelBase {
|
|
constructor(id) {
|
|
this.clients = [];
|
|
this.id = id;
|
|
logger.accessLog.info('Channel Created', { channelId: id });
|
|
}
|
|
addClient(client) {
|
|
if (this.clientExists(client.id)) {
|
|
logger.errorLog.info('Client already exits in channel', { channelId: this.id, clientId: client.id });
|
|
return { 'status': 'notice', 'message': 'client aleady exists in channel' };
|
|
}
|
|
else {
|
|
this.clients.push(client);
|
|
logger.accessLog.info('Added client to channel', { channelId: this.id, clientId: client.id });
|
|
return { 'status': 'success', 'message': 'client added' };
|
|
}
|
|
}
|
|
clientExists(id) {
|
|
for (let client of this.clients) {
|
|
if (client.id == id) {
|
|
return client;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
removeClient(id) {
|
|
var index = 0;
|
|
for (let client of this.clients) {
|
|
if (client.id == id) {
|
|
this.clients.splice(index, 1);
|
|
return true;
|
|
}
|
|
index++;
|
|
}
|
|
return false;
|
|
}
|
|
broadcastMessage(from, message) {
|
|
for (let client of this.clients) {
|
|
if (client != from) {
|
|
client.ws.send(message);
|
|
logger.accessLog.info(`sent to ${client.id}: `, { message: message });
|
|
}
|
|
else {
|
|
logger.accessLog.info(`client is same as sender: ${client.id} - `, { message: message });
|
|
}
|
|
}
|
|
return { 'status': 'success', 'message': `message broadcast complete` };
|
|
}
|
|
}
|
|
;
|
|
exports.default = ChannelBase;
|
|
//# sourceMappingURL=channelBase.js.map
|