From be381ea43822f84e2dca6b8d7126ffa781a03e6a Mon Sep 17 00:00:00 2001 From: schlagmichdoch Date: Thu, 16 May 2024 19:44:43 +0200 Subject: [PATCH] When switching public rooms disconnect from devices in old room (fixes #298) --- public/scripts/network.js | 8 +++++++- public/scripts/ui.js | 31 ++++++++++++++++--------------- server/ws-server.js | 3 +-- 3 files changed, 24 insertions(+), 18 deletions(-) diff --git a/public/scripts/network.js b/public/scripts/network.js index be2ae21..2e5d036 100644 --- a/public/scripts/network.js +++ b/public/scripts/network.js @@ -1502,9 +1502,12 @@ class PeersManager { Events.on('peer-connected', e => this._onPeerConnected(e.detail.peerId)); Events.on('peer-disconnected', e => this._onPeerDisconnected(e.detail)); + // ROOMS + Events.on('join-public-room', e => this._onJoinPublicRoom(e.detail.roomId)); + // this device closes connection Events.on('room-secrets-deleted', e => this._onRoomSecretsDeleted(e.detail)); - Events.on('leave-public-room', e => this._onLeavePublicRoom(e.detail)); + Events.on('leave-public-room', _ => this._onLeavePublicRoom()); // peer closes connection Events.on('secret-room-deleted', e => this._onSecretRoomDeleted(e.detail)); @@ -1682,6 +1685,9 @@ class PeersManager { } _onJoinPublicRoom(roomId) { + if (roomId !== this._device.publicRoomId) { + this._disconnectFromPublicRoom(); + } this._device.publicRoomId = roomId; } diff --git a/public/scripts/ui.js b/public/scripts/ui.js index 10b8f46..b70cb1d 100644 --- a/public/scripts/ui.js +++ b/public/scripts/ui.js @@ -2046,8 +2046,8 @@ class PublicRoomDialog extends Dialog { Events.on('keydown', e => this._onKeyDown(e)); Events.on('public-room-created', e => this._onPublicRoomCreated(e.detail)); - Events.on('peers', e => this._onPeers(e.detail)); - Events.on('peer-joined', e => this._onPeerJoined(e.detail.peer, e.detail.roomId)); + Events.on('peers', e => this._onPeers(e.detail.peers, e.detail.roomId)); + Events.on('peer-joined', e => this._onPeerJoined(e.detail.roomId)); Events.on('public-room-id-invalid', e => this._onPublicRoomIdInvalid(e.detail)); Events.on('public-room-left', _ => this._onPublicRoomLeft()); this.$el.addEventListener('paste', e => this._onPaste(e)); @@ -2177,29 +2177,30 @@ class PublicRoomDialog extends Dialog { } } - _onPeers(message) { - message.peers.forEach(messagePeer => { - this._evaluateJoinedPeer(messagePeer.id, message.roomId); - }); + _onPeers(peers, roomId) { + // Do not evaluate if creating new room + if (this.roomId && !peers.length) return; + + this._evaluateJoinedPeer(roomId); } - _onPeerJoined(peer, roomId) { - this._evaluateJoinedPeer(peer.id, roomId); + _onPeerJoined(roomId) { + this._evaluateJoinedPeer(roomId); } - _evaluateJoinedPeer(peerId, roomId) { - const isInitiatedRoomId = roomId === this.roomId; - const isJoinedRoomId = roomId === this.roomIdJoin; + _evaluateJoinedPeer(roomId) { + const peerJoinedThisRoom = roomId === this.roomId; + const switchedToOtherRoom = roomId === this.roomIdJoin; - if (!peerId || !roomId || (!isInitiatedRoomId && !isJoinedRoomId)) return; + if (!roomId || (!peerJoinedThisRoom && !switchedToOtherRoom)) return; this.hide(); sessionStorage.setItem('public_room_id', roomId); - if (isJoinedRoomId) { + if (switchedToOtherRoom) { + this.roomIdJoin = null; this.roomId = roomId; - this.roomIdJoin = false; this._setKeyAndQrCode(); } } @@ -2212,7 +2213,7 @@ class PublicRoomDialog extends Dialog { } _leavePublicRoom() { - Events.fire('leave-public-room', this.roomId); + Events.fire('leave-public-room'); } _onPublicRoomLeft() { diff --git a/server/ws-server.js b/server/ws-server.js index 589c424..a55b55d 100644 --- a/server/ws-server.js +++ b/server/ws-server.js @@ -251,7 +251,6 @@ export default class PairDropWsServer { return; } - this._leavePublicRoom(sender); this._joinPublicRoom(sender, message.publicRoomId); } @@ -312,7 +311,7 @@ export default class PairDropWsServer { _joinPublicRoom(peer, publicRoomId) { // prevent joining of 2 public rooms simultaneously - this._leavePublicRoom(peer); + this._leavePublicRoom(peer, true); this._joinRoom(peer, 'public-id', publicRoomId);