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; }; /** * @type {Map} */ 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;