Merge pull request #11 from yardstick/YASDEV-389-add-channel-content-to-custom-channels

YASDEV-389: initial additions required for channel content
This commit is contained in:
brmnjsh 2020-02-21 13:05:42 -05:00 committed by GitHub
commit 5d45b9e776
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 78 additions and 35 deletions

View File

@ -47,11 +47,20 @@ class ChannelManager {
if (channel) {
channel.addClient(client);
client.connectToChannel(channel);
return {status: 'success'};
} else {
logger.accessLog.info(`channel with id ${channel_id} does not exist.`);
return {status: 'notice', message: `channel with id ${channel_id} does not exist.`};
logger.accessLog.info(`channel with id ${channel_id} does not exist. could not add client to channel`);
return {status: 'notice', message: `channel with id ${channel_id} does not exist. could not add client to channel`};
}
}
updateChannelContent(channel: PrivateChannel|PrivateChannel|CustomChannel, channelContent: JSON) {
if (channel) {
channel.channelContent = channelContent;
return {status: 'success'};
} else {
logger.accessLog.info(`channel with id ${channel} does not exist. could not update content`);
return {status: 'notice', message: `channel with id ${channel} does not exist. could not update content`};
}
}
@ -106,7 +115,9 @@ class ChannelManager {
purgeEmptyChannels() {
for (const channel of this.channels) {
if (channel.clients.length === 0 && channel.id !== 'default') {
logger.accessLog.info(`Purging empty channels...`);
if (channel.clients.length === 0 && channel.explicitRemoval === false) {
const index = this.channels.indexOf(channel);
this.channels.splice(index, 1);
logger.accessLog.info(`channel removed: ${channel.id}`);

View File

@ -7,6 +7,9 @@ const logger = require('../logger');
class ChannelBase {
id: string;
clients: any[] = [];
broadcastConditions: string = "true";
channelContent: JSON = JSON.parse('{}');
explicitRemoval: boolean = false;
constructor(id: string) {
this.id = id;
@ -26,8 +29,8 @@ class ChannelBase {
return {status: 'success', message: `message broadcast complete`};
}
messageTransactionPossible(_from: PublicClient|PrivateClient|CustomClient, _to: PublicClient|PrivateClient|CustomClient) {
return true;
messageTransactionPossible(from: PublicClient|PrivateClient|CustomClient, to: PublicClient|PrivateClient|CustomClient) {
return eval(this.broadcastConditions);
}
addClient(client: PublicClient|PrivateClient|CustomClient) {
@ -36,6 +39,7 @@ class ChannelBase {
return {status: 'notice', message: 'client already exists in channel'};
} else {
this.clients.push(client);
client.connectToChannel(this);
logger.accessLog.info('Added client to channel', {channelId: this.id, clientId: client.id});
return {status: 'success', message: 'client added'};
}

View File

@ -3,15 +3,15 @@ import ChannelBase from '../channelBase';
class CustomChannel extends ChannelBase {
clients: CustomClient[] = [];
custom: any;
constructor(id: string, custom: any) {
super(id);
this.custom = custom;
}
messageTransactionPossible(from: CustomClient, to: CustomClient) {
return eval(this.custom.broadcastConditions);
if (custom) {
this.broadcastConditions = custom.broadcastConditions;
this.channelContent = custom.channelContent;
this.explicitRemoval = custom.explicitRemoval;
}
}
}

View File

@ -4,12 +4,13 @@ import ChannelBase from '../channelBase';
class PrivateChannel extends ChannelBase {
clients: PrivateClient[] = [];
messageTransactionPossible(from: PrivateClient, to: PrivateClient) {
return (
to !== from &&
to.roles.includes('receiver') &&
from.roles.includes('broadcaster')
);
constructor(id: string) {
super(id);
this.broadcastConditions =
"to != from && \
to.roles.includes('receiver') && \
from.roles.includes('broadcaster')";
}
}

View File

@ -4,10 +4,10 @@ import ChannelBase from '../channelBase';
class PublicChannel extends ChannelBase {
clients: PublicClient[] = [];
messageTransactionPossible(from: PublicClient, to: PublicClient) {
return (
to !== from
);
constructor(id: string) {
super(id);
this.broadcastConditions = "to != from";
}
}

View File

@ -17,6 +17,9 @@ class ClientBase {
channelManager: ChannelManager;
roles: string[];
messageListener: (data: any) => void;
closeListener: () => void;
pongListener: () => void;
heartbeat: any;
constructor(data: any, ws: WebSocket, channelManager: ChannelManager, clientManager: ClientManager) {
this.ws = ws;
@ -33,6 +36,22 @@ class ClientBase {
this.messageTransaction(message);
}
};
this.closeListener = () => {
logger.accessLog.info(`closing connection for client ${this.id}`);
if (this.channel) {
this.channel.removeClient(this.id);
}
this.clientManager.removeClient(this.id);
clearInterval(this.heartbeat);
logger.accessLog.info(`closed connection for client ${this.id}`);
};
this.pongListener = () => {
logger.accessLog.info(`client (${this.id}) ponged.`);
this.ws.pong();
}
this.heartbeat = setInterval(() => { this.ws.ping('ping') }, 30000);
logger.accessLog.info('Client Created', {data});
}
@ -51,19 +70,10 @@ class ClientBase {
connectToChannel(channel: PublicChannel|PrivateChannel|CustomChannel) {
this.channel = channel;
this.ws.on('message', this.messageListener);
this.ws.on('close', (reasonCode: string, description: string) => {
logger.accessLog.info(`closing connection for client ${this.id}`);
if (this.channel) {
this.channel.removeClient(this.id);
}
this.clientManager.removeClient(this.id);
logger.accessLog.info(`closed connection for client ${this.id}`);
});
this.ws.on('close', this.closeListener);
this.ws.on('pong', this.pongListener);
this.heartbeat;
}
messageTransaction(message: any) {

View File

@ -41,9 +41,26 @@ function connectionManager() {
client = clientManager.addClient(data, channelManager, ws);
}
if (client != null) channelManager.addClientToChannel(client, data.channel);
if (client != null) {
channelManager.addClientToChannel(client, data.channel);
if (data.channel_type == 'custom') {
const channel = channelManager.channelExists(data.channel);
let connectionResponse = {
message_type: 'channelStatus',
content: channel.channelContent
}
if (data.user_roles.includes('super')) {
channelManager.updateChannelContent(channel, data.custom.channelContent);
connectionResponse['content'] = channel.channelContent;
channel.broadcastMessage(client, connectionResponse);
} else {
client.directMessage(connectionResponse);
}
}
}
logger.accessLog.info(`Purging empty channels...`);
channelManager.purgeEmptyChannels();
ws.send(`Hi there, welcome to braid, Measures Web Socket server. Connecting all our services!\nYou are currently in channel: ${data.channel}`);