ping pong check, fix to channel status, explicit channel removal option added for custom channels

This commit is contained in:
Josh Burman 2020-02-13 13:19:52 -05:00
parent 3b98db62a2
commit 030fb62b38
5 changed files with 18 additions and 4 deletions

View File

@ -115,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

@ -9,6 +9,7 @@ class ChannelBase {
clients: any[] = [];
broadcastConditions: string = "true";
channelContent: JSON = JSON.parse('{}');
explicitRemoval: boolean = false;
constructor(id: string) {
this.id = id;

View File

@ -10,6 +10,7 @@ class CustomChannel extends ChannelBase {
if (custom) {
this.broadcastConditions = custom.broadcastConditions;
this.channelContent = custom.channelContent;
this.explicitRemoval = custom.explicitRemoval;
}
}
}

View File

@ -18,6 +18,8 @@ class ClientBase {
roles: string[];
messageListener: (data: any) => void;
closeListener: () => void;
pongListener: () => void;
heartbeat: any;
constructor(data: any, ws: WebSocket, channelManager: ChannelManager, clientManager: ClientManager) {
this.ws = ws;
@ -42,8 +44,14 @@ class ClientBase {
}
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});
}
@ -64,6 +72,8 @@ class ClientBase {
this.channel = channel;
this.ws.on('message', this.messageListener);
this.ws.on('close', this.closeListener);
this.ws.on('pong', this.pongListener);
this.heartbeat;
}
messageTransaction(message: any) {

View File

@ -46,21 +46,21 @@ function connectionManager() {
if (data.channel_type == 'custom') {
const channel = channelManager.channelExists(data.channel);
const connectionResponse = {
let connectionResponse = {
message_type: 'channelStatus',
content: channel.channelContent
}
if (data.user_roles.includes('super')) {
channelManager.updateChannelContent(channel, data.custom.channelContent);
channel.broadcastMessage(connectionResponse);
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}`);