From 4cd68f4a7f567d53bedeb6a1d52efff62f0077d6 Mon Sep 17 00:00:00 2001 From: Abdussamed Date: Fri, 5 Apr 2024 10:06:23 +0300 Subject: [PATCH] Disconnected pair event added --- Source/Client.js | 8 ++++++-- Source/Services/Auth.js | 15 ++++++++++++++- Source/Services/DataTransfer.js | 33 +++++++++++++++++++++++++-------- frontend/index.ts | 15 ++++++++++----- 4 files changed, 55 insertions(+), 16 deletions(-) diff --git a/Source/Client.js b/Source/Client.js index 6d57832..ffcc095 100644 --- a/Source/Client.js +++ b/Source/Client.js @@ -125,7 +125,7 @@ Client.prototype.isSecure = function(client) return false; } /** - * @returns {{pairs:Map,roompairs:Map}} + * @returns {{pairs:Map,roompairs:Map,intersection:Map}} */ Client.prototype.getSucureClients = function() { @@ -152,7 +152,11 @@ Client.prototype.getSucureClients = function() }; return { pairs, - roompairs + roompairs, + intersection : new Map([ + ...pairs, + ...roompairs + ]) }; } diff --git a/Source/Services/Auth.js b/Source/Services/Auth.js index 46d3ddd..e5ebc6f 100644 --- a/Source/Services/Auth.js +++ b/Source/Services/Auth.js @@ -1,6 +1,19 @@ const { Client } = require("../Client.js"); let {addService, addListener} = require("../WebSocket.js"); +addListener('disconnect',(global, xclient)=>{ + const {intersection} = xclient.getSucureClients(); + for (const [clientid, client] of intersection) + { + client.send([ + { + id: clientid + }, + "peer/disconnect" + ]) + } +}); + addService(({ client, message, @@ -62,7 +75,7 @@ addService(({ message: 'ALREADY-PAIRED' }) } - if(client.pairs.add(to)) + if(client.pairs.has(to)) { return end({ status: 'fail', diff --git a/Source/Services/DataTransfer.js b/Source/Services/DataTransfer.js index c9c6972..b7b974a 100644 --- a/Source/Services/DataTransfer.js +++ b/Source/Services/DataTransfer.js @@ -38,12 +38,21 @@ addService(({ if(Client.clients.has(to)) { let otherPeer = Client.clients.get(to); - if(otherPeer.requiredPair && !otherPeer.pairs.has(to)) + if(otherPeer.requiredPair) { - console.error("Client, güvenilir olmayan bir cliente mesaj iletiyor") - return handshake && end({ - type: 'fail' - }) + if(!otherPeer.pairs.has(to)) + { + console.error("Client, güvenilir olmayan bir cliente mesaj iletiyor") + return handshake && end({ + type: 'fail' + }) + } + }else{ + if(!otherPeer.pairs.has(to)) + { + otherPeer.pairs.add(client.id); + client.pairs.add(otherPeer.id); + } } if(!otherPeer.packWriteable()){ @@ -72,10 +81,18 @@ addService(({ if(Client.clients.has(to)) { let otherPeer = Client.clients.get(to); - if(otherPeer.requiredPair && !otherPeer.pairs.has(to)) + if(otherPeer.requiredPair) { - console.error("response istenen peer güvenli değil") - return; + if(!otherPeer.pairs.has(to)) + { + console.error("Client, güvenilir olmayan bir clientden veri istiyor") + return handshake && end({ + type: 'fail' + }) + } + }else{ + otherPeer.pairs.add(client.id); + client.pairs.add(otherPeer.id); } otherPeer.send([{ from: client.id, diff --git a/frontend/index.ts b/frontend/index.ts index 23773f5..d480ae2 100644 --- a/frontend/index.ts +++ b/frontend/index.ts @@ -149,7 +149,7 @@ export default class MWSE extends EventTarget { this.EventPooling.signal('room/ejected',(payload : {id:string,roomid:any,ownerid:string}) => { let {id,roomid} = payload; let room = this.room(roomid); - let peer = this.peer(id); + let peer = this.peer(id, true); room.peers.delete(peer.socketId as string); room.emit('eject', peer); }) @@ -162,27 +162,32 @@ export default class MWSE extends EventTarget { }) this.EventPooling.signal("pair/info", (payload : {from : string,name: string, value: string | number | boolean}) => { let {from, name, value} = payload; - let peer = this.peer(from); + let peer = this.peer(from, true); peer.info.info[name] = value; peer.emit("info", name, value); }) this.EventPooling.signal("request/pair", (payload : {from : string,info: any}) => { let {from, info} = payload; - let peer = this.peer(from); + let peer = this.peer(from, true); peer.info.info = info; peer.emit("request/pair", peer); this.peer('me').emit('request/pair', peer); }) + this.EventPooling.signal("peer/disconnect", (payload : {id : string}) => { + let {id} = payload; + let peer = this.peer(id, true); + peer.emit("disconnect", peer); + }) this.EventPooling.signal("accepted/pair", (payload : {from : string,info: any}) => { let {from, info} = payload; - let peer = this.peer(from); + let peer = this.peer(from, true); peer.info.info = info; peer.emit("accepted/pair", peer); this.peer('me').emit('accepted/pairr', peer); }) this.EventPooling.signal("end/pair", (payload : {from : string,info: any}) => { let {from, info} = payload; - let peer = this.peer(from); + let peer = this.peer(from, true); peer.emit("endPair", info); this.peer('me').emit('endPair', from, info); })