81 lines
1.7 KiB
JavaScript
81 lines
1.7 KiB
JavaScript
function Client()
|
|
{
|
|
/**
|
|
* @type {string}
|
|
*/
|
|
this.id = null;
|
|
/**
|
|
* @type {import("websocket").connection}
|
|
*/
|
|
this.socket = null;
|
|
/**
|
|
* @type {Date}
|
|
*/
|
|
this.created_at = null;
|
|
|
|
this.store = new Map();
|
|
this.rooms = new Set();
|
|
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.store.forEach((value, name) => info[name] = value);
|
|
this.pairs.add(client.id);
|
|
client.send([{
|
|
from: this.id,
|
|
info
|
|
},'request/pair']);
|
|
};
|
|
|
|
/**
|
|
* @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
|
|
},'rejected/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);
|
|
};
|
|
Client.prototype.pairList = function(){
|
|
return [...this.pairs.values()].filter(e => this.isPaired(e));
|
|
};
|
|
|
|
Client.prototype.send = function(obj){
|
|
this.socket.sendUTF(JSON.stringify(obj));
|
|
};
|
|
|
|
exports.Client = Client; |