213 lines
4.6 KiB
JavaScript
213 lines
4.6 KiB
JavaScript
function Client()
|
||
{
|
||
/**
|
||
* @type {string}
|
||
*/
|
||
this.id = null;
|
||
/**
|
||
* @type {import("websocket").connection}
|
||
*/
|
||
this.socket = null;
|
||
/**
|
||
* @type {Date}
|
||
*/
|
||
this.created_at = null;
|
||
|
||
/**
|
||
* @type {Map<string,any>}
|
||
*/
|
||
this.info = new Map();
|
||
/**
|
||
* @type {Map<string,any>}
|
||
*/
|
||
this.store = new Map();
|
||
/**
|
||
* @type {Set<string>}
|
||
*/
|
||
this.rooms = new Set();
|
||
/**
|
||
* @type {Set<string>}
|
||
*/
|
||
this.pairs = new Set();
|
||
this.requiredPair = false;
|
||
|
||
this.APNumber = 0;
|
||
this.APShortCode = 0;
|
||
this.APIPAddress = 0;
|
||
};
|
||
/**
|
||
* @type {Map<string, Client>}
|
||
*/
|
||
Client.clients = new Map();
|
||
|
||
/**
|
||
* @param {Client} client
|
||
*/
|
||
Client.prototype.peerRequest = function(client){
|
||
let info = {};
|
||
this.info.forEach((value, name) => info[name] = value);
|
||
this.pairs.add(client.id);
|
||
client.send([
|
||
{ from: this.id },
|
||
'request/pair'
|
||
]);
|
||
};
|
||
|
||
Client.prototype.match = function(filterObject){
|
||
let keys = Object.keys(filterObject);
|
||
let size = keys.length;
|
||
if(size > this.info.size)
|
||
{
|
||
return false
|
||
}
|
||
for (const key of keys)
|
||
{
|
||
if(this.info.has(key))
|
||
{
|
||
if(this.info.get(key) != filterObject[key])
|
||
{
|
||
return false
|
||
}
|
||
}else{
|
||
return false
|
||
}
|
||
};
|
||
return true
|
||
};
|
||
|
||
/**
|
||
* @param {Client|string} client
|
||
*/
|
||
Client.prototype.isSecure = function(client)
|
||
{
|
||
const { Room } = require("./Services/Room");
|
||
if(typeof client == "string")
|
||
{
|
||
if(Client.clients.has(client))
|
||
{
|
||
client = Client.clients.get(client);
|
||
}else return false;
|
||
}else if(!(client instanceof Client)){
|
||
console.error("isSecure Client bir client veri tipinde değil")
|
||
return false;
|
||
};
|
||
|
||
// Eşleştirilmiş kullanıcı
|
||
if(this.isPaired(client))
|
||
{
|
||
return true;
|
||
}
|
||
|
||
// Aynı odada bulunan kullanıcı
|
||
for (const id of this.rooms) {
|
||
let room = Room.rooms.get(id);
|
||
if(room)
|
||
{
|
||
if(room.clients.has(id))
|
||
{
|
||
return true
|
||
}
|
||
}
|
||
};
|
||
return false;
|
||
}
|
||
/**
|
||
* @returns {{pairs:Map<string, Client>,roompairs:Map<string, Client>,intersection:Map<string, Client>}}
|
||
*/
|
||
Client.prototype.getSucureClients = function()
|
||
{
|
||
const { Room } = require("./Services/Room");
|
||
let pairs = new Map();
|
||
let roompairs = new Map();
|
||
|
||
for (const id of this.pairs)
|
||
{
|
||
pairs.set(id, Client.clients.get(id))
|
||
}
|
||
|
||
// Aynı odada bulunan kullanıcı
|
||
for (const id of this.rooms) {
|
||
let room = Room.rooms.get(id);
|
||
if(room)
|
||
{
|
||
for (const [id, client] of room.clients)
|
||
{
|
||
if(id == this.id) continue;
|
||
roompairs.set(id, client)
|
||
};
|
||
}
|
||
};
|
||
return {
|
||
pairs,
|
||
roompairs,
|
||
intersection : new Map([
|
||
...pairs,
|
||
...roompairs
|
||
])
|
||
};
|
||
}
|
||
|
||
/**
|
||
* @param {Client} client
|
||
*/
|
||
Client.prototype.acceptPeerRequest = function(client){
|
||
this.pairs.add(client.id);
|
||
client.send([{
|
||
from: this.id
|
||
},'accepted/pair']);
|
||
};
|
||
/**
|
||
* @param {Client} client
|
||
*/
|
||
Client.prototype.rejectPeerRequest = function(client){
|
||
this.pairs.delete(client.id);
|
||
client.pairs.delete(this.id);
|
||
client.send([{
|
||
from: this.id
|
||
},'end/pair']);
|
||
};
|
||
/**
|
||
* @param {Client|string} client
|
||
* @returns {Boolean}
|
||
*/
|
||
Client.prototype.isPaired = function(client){
|
||
if(typeof client == "string")
|
||
{
|
||
return Client.clients.get(client)?.pairs.has(this.id) && this.pairs.has(client)
|
||
}
|
||
return client.pairs.has(this.id) && this.pairs.has(client.id);
|
||
};
|
||
/**
|
||
* @returns {string[]}
|
||
*/
|
||
Client.prototype.pairList = function(){
|
||
return [...this.pairs.values()].filter(e => this.isPaired(e));
|
||
};
|
||
|
||
Client.prototype.send = function(obj){
|
||
if(this.socket.connected){
|
||
this.socket.sendUTF(JSON.stringify(obj),err => {
|
||
if(err && this.socket)
|
||
{
|
||
console.error("I/O: Hatalı yazma işlemi yapıldı",err.message)
|
||
}
|
||
});
|
||
}else{
|
||
console.error("Bağlantısı kopmuş yazma işlemi")
|
||
}
|
||
};
|
||
|
||
Client.prototype.packWriteable = function(){
|
||
return !!this.store.get("packrecaive")
|
||
}
|
||
Client.prototype.packReadable = function(){
|
||
return !!this.store.get("packsending")
|
||
}
|
||
Client.prototype.peerInfoNotifiable = function(){
|
||
return !!this.store.get("notifyPairInfo")
|
||
}
|
||
Client.prototype.roomInfoNotifiable = function(){
|
||
return !!this.store.get("notifyRoomInfo")
|
||
}
|
||
|
||
exports.Client = Client; |