update to types
This commit is contained in:
@ -1,5 +1,54 @@
|
||||
class ChannelManager {
|
||||
import ChannelBase from './channels/channelBase';
|
||||
import ClientBase from './clients/clientBase';
|
||||
import MHSClient from './clients/sites/mhsClient';
|
||||
import MHSChannel from './channels/sites/mhsChannel';
|
||||
|
||||
class ChannelManager {
|
||||
channels: ChannelBase[]|MHSChannel[] = [];
|
||||
|
||||
constructor() {
|
||||
// create default channel...
|
||||
var channel: ChannelBase = new ChannelBase('default');
|
||||
this.channels.push(channel);
|
||||
}
|
||||
|
||||
createChannel(data: any) {
|
||||
var channel: ChannelBase|MHSChannel|null = this.channelExists(data.channel_id);
|
||||
|
||||
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);
|
||||
console.log('added channel to channel manager');
|
||||
}
|
||||
|
||||
console.log(data.channel_id);
|
||||
}
|
||||
|
||||
channelExists(channel_id: string) {
|
||||
for (let channel of this.channels) {
|
||||
if (channel.id == channel_id) {
|
||||
return channel;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
addClientToChannel(client: ClientBase|MHSClient, channel_id: string) {
|
||||
var channel: ChannelBase|MHSChannel|null = this.channelExists(channel_id);
|
||||
|
||||
if (channel) {
|
||||
channel.addClient(client);
|
||||
client.connectToChannel(channel);
|
||||
} else {
|
||||
console.log(`channel with id ${channel_id} does not exist.`)
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = ChannelManager;
|
||||
export default ChannelManager;
|
||||
|
||||
// { :client => 'mhs', :client_type => 'site', :user_id => 125, :user_type => 'user', :channel => 'mhs_1239' }
|
||||
|
46
src/channels/channelBase.ts
Normal file
46
src/channels/channelBase.ts
Normal file
@ -0,0 +1,46 @@
|
||||
import ClientBase from '../clients/clientBase';
|
||||
import MHSClient from '../clients/sites/mhsClient';
|
||||
|
||||
class ChannelBase {
|
||||
id: string;
|
||||
clients: MHSClient[]|ClientBase[] = [];
|
||||
|
||||
constructor(id: string) {
|
||||
this.id = id;
|
||||
console.log('channel created');
|
||||
}
|
||||
|
||||
addClient(client: MHSClient|ClientBase) {
|
||||
console.log(client.data)
|
||||
if (this.clientExists(client.id)) {
|
||||
console.log('client already exits in channel');
|
||||
} else {
|
||||
this.clients.push(client);
|
||||
console.log('added client to channel');
|
||||
}
|
||||
}
|
||||
|
||||
clientExists(id: number) {
|
||||
for (let client of this.clients) {
|
||||
if (client.id == id) {
|
||||
return client;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
broadcastMessage(from: ClientBase|MHSClient|null, message: string) {
|
||||
for (let client of this.clients) {
|
||||
if (client != from) {
|
||||
client.ws.send(message);
|
||||
console.log(`sent to ${client.id}: %s`, message);
|
||||
console.log(message);
|
||||
} else {
|
||||
console.log('client is same as sender');
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export default ChannelBase;
|
19
src/channels/sites/mhsChannel.ts
Normal file
19
src/channels/sites/mhsChannel.ts
Normal file
@ -0,0 +1,19 @@
|
||||
import ClientBase from '../../clients/clientBase';
|
||||
import MHSClient from '../../clients/sites/mhsClient';
|
||||
import ChannelBase from '../channelBase';
|
||||
|
||||
class MHSChannel extends ChannelBase {
|
||||
broadcastMessage(from: ClientBase|MHSClient|null, message: string) {
|
||||
for (let client of this.clients) {
|
||||
if (client != from) {
|
||||
client.ws.send(message);
|
||||
console.log(`sent to ${client.id}: %s`, message);
|
||||
console.log(message);
|
||||
} else {
|
||||
console.log('client is same as sender');
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
export default MHSChannel;
|
@ -1,22 +1,72 @@
|
||||
class ClientManager {
|
||||
ws: any;
|
||||
import * as WebSocket from 'ws';
|
||||
import ClientBase from './clients/clientBase';
|
||||
import MHSClient from './clients/sites/mhsClient';
|
||||
|
||||
constructor(data: object, ws: object) {
|
||||
this.ws = ws;
|
||||
this.client(data);
|
||||
class ClientManager {
|
||||
clients: ClientBase[]|MHSClient[] = [];
|
||||
|
||||
constructor() {
|
||||
//...maybe one day
|
||||
}
|
||||
|
||||
client(data: any) {
|
||||
if (data.site) {
|
||||
console.log('legit: ' + data.site)
|
||||
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);
|
||||
this.clients.push(client);
|
||||
console.log('client added to client manager');
|
||||
return client;
|
||||
} else {
|
||||
console.log('no client type designated, socket disconnect.')
|
||||
this.ws.close();
|
||||
console.log('no client type designated or client already exists, socket disconnected.')
|
||||
ws.close();
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
clientsOfType(client_type: string) {
|
||||
var result: ClientBase[]|MHSClient[] = [];
|
||||
|
||||
for (let client of this.clients) {
|
||||
if (client.type() == client_type) {
|
||||
result.push(client);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
clientExists(id: number) {
|
||||
for (let client of this.clients) {
|
||||
if (client.id == id) {
|
||||
return client;
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
getClient(id: number) {
|
||||
return this.clientExists(id);
|
||||
}
|
||||
|
||||
removeClient(id: number) {
|
||||
var index: number = 0;
|
||||
|
||||
for (let client of this.clients) {
|
||||
if (client.id == id) {
|
||||
client.ws.close();
|
||||
this.clients.splice(index, 1);
|
||||
console.log('client disconnected');
|
||||
return true;
|
||||
}
|
||||
|
||||
index++;
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = ClientManager;
|
||||
export default ClientManager;
|
||||
|
||||
|
||||
// user_type: 'user', user_id: 125, site: 'mhs' }
|
||||
// { :client => 'mhs', :client_type => 'site', :user_id => 125, :user_type => 'user' }
|
||||
// var exampleSocket = new WebSocket("wss://ysbraid.localhost:8443?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJkYXRhIjp7ImNsaWVudCI6Im1ocyIsImNsaWVudF90eXBlIjoic2l0ZSIsInVzZXJfdHlwZSI6InVzZXIiLCJ1c2VyX2lkIjoxMjV9LCJleHAiOjE1NTI4MDExMDUsImF1ZCI6ImludGVybmFsIiwiaXNzIjoiWWFyZHN0aWNrIFNvZnR3YXJlIiwic3ViIjoiQnJhaWQgSldUIn0.fkfoqoWNjOeKfsXXJxh-9lBudhFsxhUt9fUIT1BXOLU");
|
||||
|
42
src/clients/clientBase.ts
Normal file
42
src/clients/clientBase.ts
Normal file
@ -0,0 +1,42 @@
|
||||
import * as WebSocket from 'ws';
|
||||
import * as ChannelManager from '../channelManager';
|
||||
import ChannelBase from '../channels/channelBase';
|
||||
import MHSChannel from '../channels/sites/mhsChannel';
|
||||
|
||||
class ClientBase {
|
||||
ws: WebSocket;
|
||||
data: any;
|
||||
id: number;
|
||||
channel: ChannelBase|MHSChannel|null;
|
||||
|
||||
constructor(data: any, ws: WebSocket) {
|
||||
this.ws = ws;
|
||||
this.data = data;
|
||||
this.id = data.user_id;
|
||||
this.channel = null;
|
||||
}
|
||||
|
||||
getData() {
|
||||
return this.data;
|
||||
}
|
||||
|
||||
type() {
|
||||
return this.data.client_type;
|
||||
}
|
||||
|
||||
connectToChannel(channel: ChannelBase|MHSChannel) {
|
||||
this.channel = channel;
|
||||
|
||||
this.ws.on('message', (message: string) => {
|
||||
channel.broadcastMessage(this, message);
|
||||
console.log('broadcasted: %s', message);
|
||||
});
|
||||
}
|
||||
|
||||
replaceWebSocket(ws: WebSocket) {
|
||||
this.ws.close();
|
||||
this.ws = ws;
|
||||
}
|
||||
};
|
||||
|
||||
export default ClientBase;
|
13
src/clients/sites/mhsClient.ts
Normal file
13
src/clients/sites/mhsClient.ts
Normal file
@ -0,0 +1,13 @@
|
||||
import * as WebSocket from 'ws';
|
||||
import ClientBase from '../clientBase';
|
||||
|
||||
class MHSClient extends ClientBase {
|
||||
constructor(data: any, ws: WebSocket) {
|
||||
super(data, ws);
|
||||
console.log('mhs client created');
|
||||
}
|
||||
};
|
||||
|
||||
module.exports = MHSClient;
|
||||
export default MHSClient;
|
||||
|
@ -1,7 +1,7 @@
|
||||
module.exports = {
|
||||
version : '0.5.1',
|
||||
version : '0.5.5',
|
||||
whitelist : (process.env.WHITELIST || "http://admin.localhost").split(','),
|
||||
secret : (process.env.API_TOKENS || "test").split(','),
|
||||
secret : process.env.SECRET || "test",
|
||||
devToken: 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJkYXRhIjoidGVzdCBkYXRhIiwiYXVkIjoiaW50ZXJuYWwiLCJpc3MiOiJZYXJkc3RpY2sgU29mdHdhcmUiLCJzdWIiOiJCcmFpZCBKV1QifQ.t6LFXWPEVz3aLXwtrucarggqTkGY_2NnZB8ZTMaJ2oI',
|
||||
port: process.env.PORT || 8443,
|
||||
hostname: process.env.HOSTNAME || 'ysbraid.localhost',
|
||||
|
@ -9,6 +9,10 @@ import * as url from 'url';
|
||||
//internal imports
|
||||
var routes = require('./routes');
|
||||
var app = require('./config/app');
|
||||
import ClientManager from './clientManager';
|
||||
import ChannelManager from './channelManager';
|
||||
import ClientBase from './clients/clientBase';
|
||||
import MHSClient from './clients/sites/mhsClient';
|
||||
|
||||
var privateKey = fs.readFileSync('certs/key.pem', 'utf8');
|
||||
var certificate = fs.readFileSync('certs/cert.pem', 'utf8');
|
||||
@ -21,11 +25,30 @@ const wss = new WebSocket.Server({ noServer: true, maxPayload:250000, host: app.
|
||||
application.use(express.json());
|
||||
application.use('', routes);
|
||||
|
||||
let clientManager = new ClientManager();
|
||||
let channelManager = new ChannelManager();
|
||||
|
||||
wss.on('connection', (ws: WebSocket, request: object, args: string) => {
|
||||
console.log('client connected');
|
||||
var data = JSON.parse(args).data
|
||||
var ClientManager = require('./clientManager');
|
||||
let clientManager = new ClientManager(data, ws);
|
||||
|
||||
if (!channelManager.channelExists(data.channel)) {
|
||||
channelManager.createChannel(data);
|
||||
}
|
||||
|
||||
if (clientManager.clientExists(data.user_id)) {
|
||||
var client: ClientBase|MHSClient|null = clientManager.getClient(data.user_id);
|
||||
|
||||
if (client != null) {
|
||||
client.replaceWebSocket(ws);
|
||||
}
|
||||
} else {
|
||||
var client: ClientBase|MHSClient|null = clientManager.addClient(data, ws);
|
||||
}
|
||||
|
||||
if (client != null) {
|
||||
channelManager.addClientToChannel(client, data.channel);
|
||||
}
|
||||
|
||||
// ws.on('message', (message: string) => {
|
||||
|
||||
@ -66,7 +89,7 @@ server.on('upgrade', async function upgrade(request, socket, head) {
|
||||
function verifyConnection() {
|
||||
return new Promise((resolve, reject) => {
|
||||
var data = url.parse(request.url, true).query;
|
||||
var token = data["token"] || (app.environment == 'development' ? app.devToken : '');
|
||||
var token = data.token || (app.environment == 'development' ? app.devToken : '');
|
||||
var accepted = true;
|
||||
var result: string;
|
||||
|
||||
|
Reference in New Issue
Block a user