71 lines
2.5 KiB
JavaScript
71 lines
2.5 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
const clientBase_1 = require("./clients/clientBase");
|
|
class ClientManager {
|
|
constructor() {
|
|
this.clients = [];
|
|
//...maybe one day
|
|
}
|
|
addClient(data, ws) {
|
|
if (data.client_type && !this.clientExists(data.user_id)) {
|
|
var client = this.getClientType(data, ws);
|
|
this.clients.push(client);
|
|
console.log('client added to client manager');
|
|
return client;
|
|
}
|
|
else {
|
|
console.log('no client type designated or client already exists, socket disconnected.');
|
|
ws.close();
|
|
return null;
|
|
}
|
|
}
|
|
clientsOfType(client_type) {
|
|
var result = [];
|
|
for (let client of this.clients) {
|
|
if (client.type() == client_type) {
|
|
result.push(client);
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
clientExists(id) {
|
|
for (let client of this.clients) {
|
|
if (client.id == id) {
|
|
return client;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
getClient(id) {
|
|
return this.clientExists(id);
|
|
}
|
|
removeClient(id) {
|
|
var index = 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++;
|
|
}
|
|
}
|
|
getClientType(data, ws) {
|
|
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_1.default(data, ws);
|
|
}
|
|
}
|
|
}
|
|
;
|
|
exports.default = ClientManager;
|
|
// { :client => 'mhs', :client_type => 'site', :user_id => 125, :user_type => 'user' }
|
|
// var exampleSocket = new WebSocket("wss://ysbraid.localhost:8443?token=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJkYXRhIjp7ImNsaWVudCI6Im1ocyIsImNsaWVudF90eXBlIjoic2l0ZSIsInVzZXJfdHlwZSI6InVzZXIiLCJ1c2VyX2lkIjoxMjV9LCJleHAiOjE1NTI4MDExMDUsImF1ZCI6ImludGVybmFsIiwiaXNzIjoiWWFyZHN0aWNrIFNvZnR3YXJlIiwic3ViIjoiQnJhaWQgSldUIn0.fkfoqoWNjOeKfsXXJxh-9lBudhFsxhUt9fUIT1BXOLU");
|
|
//# sourceMappingURL=clientManager.js.map
|