if user actively disconnects from PairDrop server, disconnect all peer to peer connections immediately and do not wait for channels to close

This commit is contained in:
schlagmichdoch 2023-02-10 23:47:39 +01:00
parent 1eba7359d1
commit 414cc9aa57
3 changed files with 27 additions and 15 deletions

View file

@ -177,8 +177,8 @@ class PairDropServer {
} }
_onDisconnect(sender) { _onDisconnect(sender) {
this._leaveRoom(sender); this._leaveRoom(sender, 'ip', '', true);
this._leaveAllSecretRooms(sender); this._leaveAllSecretRooms(sender, true);
this._removeRoomKey(sender.roomKey); this._removeRoomKey(sender.roomKey);
sender.roomKey = null; sender.roomKey = null;
} }
@ -322,7 +322,7 @@ class PairDropServer {
} }
} }
_leaveRoom(peer, roomType = 'ip', roomSecret = '') { _leaveRoom(peer, roomType = 'ip', roomSecret = '', disconnect = false) {
const room = roomType === 'ip' ? peer.ip : roomSecret; const room = roomType === 'ip' ? peer.ip : roomSecret;
if (!this._rooms[room] || !this._rooms[room][peer.id]) return; if (!this._rooms[room] || !this._rooms[room][peer.id]) return;
@ -346,7 +346,8 @@ class PairDropServer {
type: 'peer-left', type: 'peer-left',
peerId: peer.id, peerId: peer.id,
roomType: roomType, roomType: roomType,
roomSecret: roomSecret roomSecret: roomSecret,
disconnect: disconnect
}); });
} }
} }
@ -393,9 +394,9 @@ class PairDropServer {
} }
} }
_leaveAllSecretRooms(peer) { _leaveAllSecretRooms(peer, disconnect = false) {
for (let i=0; i<peer.roomSecrets.length; i++) { for (let i=0; i<peer.roomSecrets.length; i++) {
this._leaveRoom(peer, 'secret', peer.roomSecrets[i]); this._leaveRoom(peer, 'secret', peer.roomSecrets[i], disconnect);
} }
} }

View file

@ -70,7 +70,7 @@ class ServerConnection {
Events.fire('peer-joined', msg); Events.fire('peer-joined', msg);
break; break;
case 'peer-left': case 'peer-left':
Events.fire('peer-left', msg.peerId); Events.fire('peer-left', msg);
break; break;
case 'signal': case 'signal':
Events.fire('signal', msg); Events.fire('signal', msg);
@ -653,6 +653,7 @@ class PeersManager {
Events.on('files-selected', e => this._onFilesSelected(e.detail)); Events.on('files-selected', e => this._onFilesSelected(e.detail));
Events.on('respond-to-files-transfer-request', e => this._onRespondToFileTransferRequest(e.detail)) Events.on('respond-to-files-transfer-request', e => this._onRespondToFileTransferRequest(e.detail))
Events.on('send-text', e => this._onSendText(e.detail)); Events.on('send-text', e => this._onSendText(e.detail));
Events.on('peer-left', e => this._onPeerLeft(e.detail));
Events.on('peer-disconnected', e => this._onPeerDisconnected(e.detail)); Events.on('peer-disconnected', e => this._onPeerDisconnected(e.detail));
Events.on('secret-room-deleted', e => this._onSecretRoomDeleted(e.detail)); Events.on('secret-room-deleted', e => this._onSecretRoomDeleted(e.detail));
} }
@ -706,6 +707,13 @@ class PeersManager {
this.peers[message.to].sendText(message.text); this.peers[message.to].sendText(message.text);
} }
_onPeerLeft(msg) {
if (msg.disconnect === true) {
// if user actively disconnected from PairDrop disconnect all peer to peer connections immediately
Events.fire('peer-disconnected', msg.peerId);
}
}
_onPeerDisconnected(peerId) { _onPeerDisconnected(peerId) {
const peer = this.peers[peerId]; const peer = this.peers[peerId];
delete this.peers[peerId]; delete this.peers[peerId];

View file

@ -68,7 +68,7 @@ class ServerConnection {
Events.fire('peer-joined', msg); Events.fire('peer-joined', msg);
break; break;
case 'peer-left': case 'peer-left':
Events.fire('peer-left', msg.peerId); Events.fire('peer-left', msg);
break; break;
case 'signal': case 'signal':
Events.fire('signal', msg); Events.fire('signal', msg);
@ -766,6 +766,16 @@ class PeersManager {
this.peers[message.to].sendText(message.text); this.peers[message.to].sendText(message.text);
} }
_onPeerLeft(msg) {
if (this.peers[msg.peerId] && !this.peers[msg.peerId].rtcSupported) {
console.log('WSPeer left:', msg.peerId)
Events.fire('peer-disconnected', msg.peerId)
} else if (msg.disconnect === true) {
// if user actively disconnected from PairDrop server, disconnect all peer to peer connections immediately
Events.fire('peer-disconnected', msg.peerId);
}
}
_onPeerDisconnected(peerId) { _onPeerDisconnected(peerId) {
const peer = this.peers[peerId]; const peer = this.peers[peerId];
delete this.peers[peerId]; delete this.peers[peerId];
@ -775,13 +785,6 @@ class PeersManager {
peer._busy = false; peer._busy = false;
} }
_onPeerLeft(peerId) {
if (!this.peers[peerId]?.rtcSupported) {
console.log('WSPeer left:', peerId)
Events.fire('peer-disconnected', peerId)
}
}
_onSecretRoomDeleted(roomSecret) { _onSecretRoomDeleted(roomSecret) {
for (const peerId in this.peers) { for (const peerId in this.peers) {
const peer = this.peers[peerId]; const peer = this.peers[peerId];