properly delegate to sub classes for channels and clients

This commit is contained in:
Josh Burman
2019-03-11 16:55:44 -04:00
parent cdd6179992
commit 74aad4a957
11 changed files with 66 additions and 44 deletions

View File

@ -13,18 +13,15 @@ class ChannelManager {
}
createChannel(data: any) {
var channel: ChannelBase|MHSChannel|null = this.channelExists(data.channel_id);
var channel: ChannelBase|MHSChannel|null = this.channelExists(data.channel);
if (channel) {
console.log('channel already exists');
} else {
var Channel = require(`./channels/${data.client_type}s/${data.client}channel`);
var channel: ChannelBase|MHSChannel|null = new ChannelBase(data.channel_id);
this.channels.push(channel);
channel = this.getChannelType(data);
channel ? this.channels.push(channel) : null;
console.log('added channel to channel manager');
}
console.log(data.channel_id);
}
channelExists(channel_id: string) {
@ -47,6 +44,18 @@ class ChannelManager {
console.log(`channel with id ${channel_id} does not exist.`)
}
}
getChannelType(data: any) {
try {
var Channel = require(`./channels/${data.client_type}s/${data.client}channel`);
console.log(`attempting to create channel of type ${data.client}, channel id: ${data.channel}...`);
return new Channel(data.channel);
} catch (e) {
console.log(e);
console.log(`creating base channel`);
return new ChannelBase(data.channel);
}
}
};
export default ChannelManager;

View File

@ -5,15 +5,16 @@ import ChannelBase from '../channelBase';
class MHSChannel extends ChannelBase {
broadcastMessage(from: ClientBase|MHSClient|null, message: string) {
for (let client of this.clients) {
if (client != from) {
if (client != from && client.data.user_type == 'teacher') {
client.ws.send(message);
console.log(`sent to ${client.id}: %s`, message);
console.log(message);
} else {
console.log('client is same as sender');
console.log('client is same as sender and/or not a teacher.');
}
}
}
};
module.exports = MHSChannel;
export default MHSChannel;

View File

@ -11,8 +11,7 @@ class ClientManager {
addClient(data: any, ws: WebSocket) {
if (data.client_type && !this.clientExists(data.user_id)) {
var Client = require(`./clients/${data.client_type}s/${data.client}client`);
var client = new Client(data, ws);
var client = this.getClientType(data, ws);
this.clients.push(client);
console.log('client added to client manager');
return client;
@ -63,6 +62,18 @@ class ClientManager {
index++;
}
}
getClientType(data: any, ws: WebSocket) {
try {
var Client = require(`./clients/${data.client_type}s/${data.client}client`);
console.log(`attempting to create client of type ${data.client}, client id: ${data.user_id}...`);
return new Client(data, ws);
} catch (e) {
console.log(e);
console.log(`creating base client`);
return new ClientBase(data, ws);
}
}
};
export default ClientManager;

View File

@ -1,5 +1,5 @@
module.exports = {
version : '0.5.5',
version : '0.6.2',
whitelist : (process.env.WHITELIST || "http://admin.localhost").split(','),
secret : process.env.SECRET || "test",
devToken: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJkYXRhIjoidGVzdCBkYXRhIiwiYXVkIjoiaW50ZXJuYWwiLCJpc3MiOiJZYXJkc3RpY2sgU29mdHdhcmUiLCJzdWIiOiJCcmFpZCBKV1QifQ.t6LFXWPEVz3aLXwtrucarggqTkGY_2NnZB8ZTMaJ2oI',

View File

@ -50,28 +50,6 @@ wss.on('connection', (ws: WebSocket, request: object, args: string) => {
channelManager.addClientToChannel(client, data.channel);
}
// ws.on('message', (message: string) => {
// console.log('received: %s', message);
// const broadcastRegex = /^broadcast\:/;
// if (broadcastRegex.test(message)) {
// message = message.replace(broadcastRegex, '');
// //send back the message to the other clients
// wss.clients
// .forEach(client => {
// if (client != ws) {
// client.send(`Hello, broadcast message -> ${message}`);
// }
// });
// } else {
// ws.send(`Hello, you sent -> ${message}`);
// }
// });
ws.send('Hi there, welcome to braid, Measures Web Socket server.\nConnecting all our services!');
});