63 lines
1.7 KiB
TypeScript
63 lines
1.7 KiB
TypeScript
import ClientBase from '../clients/clientBase';
|
|
import ServicesClient from '../clients/servicesClient';
|
|
|
|
var logger = require('../logger');
|
|
|
|
class ChannelBase {
|
|
id: string;
|
|
clients: ServicesClient[]|ClientBase[] = [];
|
|
|
|
constructor(id: string) {
|
|
this.id = id;
|
|
logger.accessLog.info('Channel Created', {channelId: id});
|
|
}
|
|
|
|
addClient(client: ServicesClient|ClientBase) {
|
|
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: number) {
|
|
for (let client of this.clients) {
|
|
if (client.id == id) {
|
|
return client;
|
|
}
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
removeClient(id: number) {
|
|
for (let client of this.clients) {
|
|
if (client.id == id) {
|
|
var index = this.clients.indexOf(client)
|
|
this.clients.splice(index, 1);
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
broadcastMessage(from: ClientBase|null, message: string) {
|
|
for (let client of this.clients) {
|
|
if (client != from) {
|
|
client.ws.send(JSON.stringify(message));
|
|
logger.accessLog.info(`sent to ${client.id}`, { data: { message: message }});
|
|
} else {
|
|
logger.accessLog.info(`client is same as sender: ${client.id}`, { data: { message: message }});
|
|
}
|
|
}
|
|
|
|
return {'status': 'success', 'message': `message broadcast complete`};
|
|
}
|
|
};
|
|
|
|
export default ChannelBase;
|