initial additions required for channel content and the appropriate feedback to client upon all connection types (channel creation/addition/removal)
This commit is contained in:
parent
faa1186f39
commit
3b98db62a2
@ -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`};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -7,6 +7,8 @@ const logger = require('../logger');
|
|||||||
class ChannelBase {
|
class ChannelBase {
|
||||||
id: string;
|
id: string;
|
||||||
clients: any[] = [];
|
clients: any[] = [];
|
||||||
|
broadcastConditions: string = "true";
|
||||||
|
channelContent: JSON = JSON.parse('{}');
|
||||||
|
|
||||||
constructor(id: string) {
|
constructor(id: string) {
|
||||||
this.id = id;
|
this.id = id;
|
||||||
@ -26,8 +28,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 +38,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,14 @@ 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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -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,7 @@ class ClientBase {
|
|||||||
channelManager: ChannelManager;
|
channelManager: ChannelManager;
|
||||||
roles: string[];
|
roles: string[];
|
||||||
messageListener: (data: any) => void;
|
messageListener: (data: any) => void;
|
||||||
|
closeListener: () => void;
|
||||||
|
|
||||||
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 +34,16 @@ 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);
|
||||||
|
logger.accessLog.info(`closed connection for client ${this.id}`);
|
||||||
|
};
|
||||||
|
|
||||||
logger.accessLog.info('Client Created', {data});
|
logger.accessLog.info('Client Created', {data});
|
||||||
}
|
}
|
||||||
@ -51,19 +62,8 @@ 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) => {
|
|
||||||
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}`);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
messageTransaction(message: any) {
|
messageTransaction(message: any) {
|
||||||
|
@ -41,7 +41,24 @@ 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);
|
||||||
|
const connectionResponse = {
|
||||||
|
message_type: 'channelStatus',
|
||||||
|
content: channel.channelContent
|
||||||
|
}
|
||||||
|
|
||||||
|
if (data.user_roles.includes('super')) {
|
||||||
|
channelManager.updateChannelContent(channel, data.custom.channelContent);
|
||||||
|
channel.broadcastMessage(connectionResponse);
|
||||||
|
} else {
|
||||||
|
client.directMessage(connectionResponse);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
logger.accessLog.info(`Purging empty channels...`);
|
logger.accessLog.info(`Purging empty channels...`);
|
||||||
channelManager.purgeEmptyChannels();
|
channelManager.purgeEmptyChannels();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user