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:
commit
5d45b9e776
@ -47,11 +47,20 @@ class ChannelManager {
|
|||||||
|
|
||||||
if (channel) {
|
if (channel) {
|
||||||
channel.addClient(client);
|
channel.addClient(client);
|
||||||
client.connectToChannel(channel);
|
|
||||||
return {status: 'success'};
|
return {status: 'success'};
|
||||||
} else {
|
} else {
|
||||||
logger.accessLog.info(`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.`};
|
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() {
|
purgeEmptyChannels() {
|
||||||
for (const channel of this.channels) {
|
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);
|
const index = this.channels.indexOf(channel);
|
||||||
this.channels.splice(index, 1);
|
this.channels.splice(index, 1);
|
||||||
logger.accessLog.info(`channel removed: ${channel.id}`);
|
logger.accessLog.info(`channel removed: ${channel.id}`);
|
||||||
|
@ -7,6 +7,9 @@ const logger = require('../logger');
|
|||||||
class ChannelBase {
|
class ChannelBase {
|
||||||
id: string;
|
id: string;
|
||||||
clients: any[] = [];
|
clients: any[] = [];
|
||||||
|
broadcastConditions: string = "true";
|
||||||
|
channelContent: JSON = JSON.parse('{}');
|
||||||
|
explicitRemoval: boolean = false;
|
||||||
|
|
||||||
constructor(id: string) {
|
constructor(id: string) {
|
||||||
this.id = id;
|
this.id = id;
|
||||||
@ -26,8 +29,8 @@ class ChannelBase {
|
|||||||
return {status: 'success', message: `message broadcast complete`};
|
return {status: 'success', message: `message broadcast complete`};
|
||||||
}
|
}
|
||||||
|
|
||||||
messageTransactionPossible(_from: PublicClient|PrivateClient|CustomClient, _to: PublicClient|PrivateClient|CustomClient) {
|
messageTransactionPossible(from: PublicClient|PrivateClient|CustomClient, to: PublicClient|PrivateClient|CustomClient) {
|
||||||
return true;
|
return eval(this.broadcastConditions);
|
||||||
}
|
}
|
||||||
|
|
||||||
addClient(client: PublicClient|PrivateClient|CustomClient) {
|
addClient(client: PublicClient|PrivateClient|CustomClient) {
|
||||||
@ -36,6 +39,7 @@ class ChannelBase {
|
|||||||
return {status: 'notice', message: 'client already exists in channel'};
|
return {status: 'notice', message: 'client already exists in channel'};
|
||||||
} else {
|
} else {
|
||||||
this.clients.push(client);
|
this.clients.push(client);
|
||||||
|
client.connectToChannel(this);
|
||||||
logger.accessLog.info('Added client to channel', {channelId: this.id, clientId: client.id});
|
logger.accessLog.info('Added client to channel', {channelId: this.id, clientId: client.id});
|
||||||
return {status: 'success', message: 'client added'};
|
return {status: 'success', message: 'client added'};
|
||||||
}
|
}
|
||||||
|
@ -3,15 +3,15 @@ import ChannelBase from '../channelBase';
|
|||||||
|
|
||||||
class CustomChannel extends ChannelBase {
|
class CustomChannel extends ChannelBase {
|
||||||
clients: CustomClient[] = [];
|
clients: CustomClient[] = [];
|
||||||
custom: any;
|
|
||||||
|
|
||||||
constructor(id: string, custom: any) {
|
constructor(id: string, custom: any) {
|
||||||
super(id);
|
super(id);
|
||||||
this.custom = custom;
|
|
||||||
}
|
|
||||||
|
|
||||||
messageTransactionPossible(from: CustomClient, to: CustomClient) {
|
if (custom) {
|
||||||
return eval(this.custom.broadcastConditions);
|
this.broadcastConditions = custom.broadcastConditions;
|
||||||
|
this.channelContent = custom.channelContent;
|
||||||
|
this.explicitRemoval = custom.explicitRemoval;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4,12 +4,13 @@ import ChannelBase from '../channelBase';
|
|||||||
class PrivateChannel extends ChannelBase {
|
class PrivateChannel extends ChannelBase {
|
||||||
clients: PrivateClient[] = [];
|
clients: PrivateClient[] = [];
|
||||||
|
|
||||||
messageTransactionPossible(from: PrivateClient, to: PrivateClient) {
|
constructor(id: string) {
|
||||||
return (
|
super(id);
|
||||||
to !== from &&
|
|
||||||
to.roles.includes('receiver') &&
|
this.broadcastConditions =
|
||||||
from.roles.includes('broadcaster')
|
"to != from && \
|
||||||
);
|
to.roles.includes('receiver') && \
|
||||||
|
from.roles.includes('broadcaster')";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4,10 +4,10 @@ import ChannelBase from '../channelBase';
|
|||||||
class PublicChannel extends ChannelBase {
|
class PublicChannel extends ChannelBase {
|
||||||
clients: PublicClient[] = [];
|
clients: PublicClient[] = [];
|
||||||
|
|
||||||
messageTransactionPossible(from: PublicClient, to: PublicClient) {
|
constructor(id: string) {
|
||||||
return (
|
super(id);
|
||||||
to !== from
|
|
||||||
);
|
this.broadcastConditions = "to != from";
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -17,6 +17,9 @@ class ClientBase {
|
|||||||
channelManager: ChannelManager;
|
channelManager: ChannelManager;
|
||||||
roles: string[];
|
roles: string[];
|
||||||
messageListener: (data: any) => void;
|
messageListener: (data: any) => void;
|
||||||
|
closeListener: () => void;
|
||||||
|
pongListener: () => void;
|
||||||
|
heartbeat: any;
|
||||||
|
|
||||||
constructor(data: any, ws: WebSocket, channelManager: ChannelManager, clientManager: ClientManager) {
|
constructor(data: any, ws: WebSocket, channelManager: ChannelManager, clientManager: ClientManager) {
|
||||||
this.ws = ws;
|
this.ws = ws;
|
||||||
@ -33,6 +36,22 @@ class ClientBase {
|
|||||||
this.messageTransaction(message);
|
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});
|
logger.accessLog.info('Client Created', {data});
|
||||||
}
|
}
|
||||||
@ -51,19 +70,10 @@ class ClientBase {
|
|||||||
|
|
||||||
connectToChannel(channel: PublicChannel|PrivateChannel|CustomChannel) {
|
connectToChannel(channel: PublicChannel|PrivateChannel|CustomChannel) {
|
||||||
this.channel = channel;
|
this.channel = channel;
|
||||||
|
|
||||||
this.ws.on('message', this.messageListener);
|
this.ws.on('message', this.messageListener);
|
||||||
|
this.ws.on('close', this.closeListener);
|
||||||
this.ws.on('close', (reasonCode: string, description: string) => {
|
this.ws.on('pong', this.pongListener);
|
||||||
logger.accessLog.info(`closing connection for client ${this.id}`);
|
this.heartbeat;
|
||||||
|
|
||||||
if (this.channel) {
|
|
||||||
this.channel.removeClient(this.id);
|
|
||||||
}
|
|
||||||
|
|
||||||
this.clientManager.removeClient(this.id);
|
|
||||||
logger.accessLog.info(`closed connection for client ${this.id}`);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
messageTransaction(message: any) {
|
messageTransaction(message: any) {
|
||||||
|
@ -41,9 +41,26 @@ function connectionManager() {
|
|||||||
client = clientManager.addClient(data, channelManager, ws);
|
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();
|
channelManager.purgeEmptyChannels();
|
||||||
|
|
||||||
ws.send(`Hi there, welcome to braid, Measures Web Socket server. Connecting all our services!\nYou are currently in channel: ${data.channel}`);
|
ws.send(`Hi there, welcome to braid, Measures Web Socket server. Connecting all our services!\nYou are currently in channel: ${data.channel}`);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user