diff --git a/index.js b/index.js index 136129b..247b9c7 100644 --- a/index.js +++ b/index.js @@ -177,8 +177,8 @@ class PairDropServer { } _onDisconnect(sender) { - this._leaveRoom(sender); - this._leaveAllSecretRooms(sender); + this._leaveRoom(sender, 'ip', '', true); + this._leaveAllSecretRooms(sender, true); this._removeRoomKey(sender.roomKey); 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; if (!this._rooms[room] || !this._rooms[room][peer.id]) return; @@ -346,7 +346,8 @@ class PairDropServer { type: 'peer-left', peerId: peer.id, 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 this._onFilesSelected(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('peer-left', e => this._onPeerLeft(e.detail)); Events.on('peer-disconnected', e => this._onPeerDisconnected(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); } + _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) { const peer = this.peers[peerId]; delete this.peers[peerId]; diff --git a/public_included_ws_fallback/scripts/network.js b/public_included_ws_fallback/scripts/network.js index c6f9a85..ad35916 100644 --- a/public_included_ws_fallback/scripts/network.js +++ b/public_included_ws_fallback/scripts/network.js @@ -68,7 +68,7 @@ class ServerConnection { Events.fire('peer-joined', msg); break; case 'peer-left': - Events.fire('peer-left', msg.peerId); + Events.fire('peer-left', msg); break; case 'signal': Events.fire('signal', msg); @@ -766,6 +766,16 @@ class PeersManager { 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) { const peer = this.peers[peerId]; delete this.peers[peerId]; @@ -775,13 +785,6 @@ class PeersManager { peer._busy = false; } - _onPeerLeft(peerId) { - if (!this.peers[peerId]?.rtcSupported) { - console.log('WSPeer left:', peerId) - Events.fire('peer-disconnected', peerId) - } - } - _onSecretRoomDeleted(roomSecret) { for (const peerId in this.peers) { const peer = this.peers[peerId];