Leave old public room when switching public rooms (fixes #276)
This commit is contained in:
parent
00f1a20177
commit
b2554f3870
|
@ -18,7 +18,7 @@ class ServerConnection {
|
|||
|
||||
Events.on('create-public-room', _ => this._onCreatePublicRoom());
|
||||
Events.on('join-public-room', e => this._onJoinPublicRoom(e.detail.roomId, e.detail.createIfInvalid));
|
||||
Events.on('leave-public-room', _ => this._onLeavePublicRoom());
|
||||
Events.on('leave-public-room', e => this._onLeavePublicRoom(e.detail.publicRoomId));
|
||||
|
||||
Events.on('offline', _ => clearTimeout(this._reconnectTimer));
|
||||
Events.on('online', _ => this._connect());
|
||||
|
@ -126,12 +126,12 @@ class ServerConnection {
|
|||
this.send({ type: 'join-public-room', publicRoomId: roomId, createIfInvalid: createIfInvalid });
|
||||
}
|
||||
|
||||
_onLeavePublicRoom() {
|
||||
_onLeavePublicRoom(publicRoomId) {
|
||||
if (!this._isConnected()) {
|
||||
setTimeout(() => this._onLeavePublicRoom(), 1000);
|
||||
return;
|
||||
}
|
||||
this.send({ type: 'leave-public-room' });
|
||||
this.send({ type: 'leave-public-room', publicRoomId: publicRoomId });
|
||||
}
|
||||
|
||||
_onMessage(message) {
|
||||
|
@ -1501,7 +1501,7 @@ class PeersManager {
|
|||
|
||||
// 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', e => this._onLeavePublicRoom(e.detail.publicRoomId));
|
||||
|
||||
// peer closes connection
|
||||
Events.on('secret-room-deleted', e => this._onSecretRoomDeleted(e.detail));
|
||||
|
@ -1664,7 +1664,7 @@ class PeersManager {
|
|||
}
|
||||
|
||||
_onRoomSecretsDeleted(roomSecrets) {
|
||||
for (let i=0; i<roomSecrets.length; i++) {
|
||||
for (let i = 0; i < roomSecrets.length; i++) {
|
||||
this._disconnectOrRemoveRoomTypeByRoomId('secret', roomSecrets[i]);
|
||||
}
|
||||
}
|
||||
|
@ -1682,7 +1682,7 @@ class PeersManager {
|
|||
|
||||
if (!peerIds.length) return;
|
||||
|
||||
for (let i=0; i<peerIds.length; i++) {
|
||||
for (let i = 0; i < peerIds.length; i++) {
|
||||
this._disconnectOrRemoveRoomTypeByPeerId(peerIds[i], roomType);
|
||||
}
|
||||
}
|
||||
|
@ -1690,7 +1690,7 @@ class PeersManager {
|
|||
_disconnectOrRemoveRoomTypeByPeerId(peerId, roomType) {
|
||||
const peer = this.peers[peerId];
|
||||
|
||||
if (!peer) return;
|
||||
if (!peer || !peer._getRoomTypes().includes(roomType)) return;
|
||||
|
||||
if (peer._getRoomTypes().length > 1) {
|
||||
peer._removeRoomType(roomType);
|
||||
|
@ -1729,13 +1729,25 @@ class PeersManager {
|
|||
}
|
||||
|
||||
_onAutoAcceptUpdated(roomSecret, autoAccept) {
|
||||
const peerId = this._getPeerIdsFromRoomId(roomSecret)[0];
|
||||
let peerIds = this._getPeerIdsFromRoomId(roomSecret);
|
||||
const peerId = this._removePeerIdsSameBrowser(peerIds)[0];
|
||||
|
||||
if (!peerId) return;
|
||||
|
||||
this.peers[peerId]._setAutoAccept(autoAccept);
|
||||
}
|
||||
|
||||
_removePeerIdsSameBrowser(peerIds) {
|
||||
let peerIdsNotSameBrowser = [];
|
||||
for (let i = 0; i < peerIds.length; i++) {
|
||||
const peer = this.peers[peerIds[i]];
|
||||
if (!peer._isSameBrowser()) {
|
||||
peerIdsNotSameBrowser.push(peerIds[i]);
|
||||
}
|
||||
}
|
||||
return peerIdsNotSameBrowser;
|
||||
}
|
||||
|
||||
_getPeerIdsFromRoomId(roomId) {
|
||||
if (!roomId) return [];
|
||||
|
||||
|
|
|
@ -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));
|
||||
|
@ -2147,7 +2147,6 @@ class PublicRoomDialog extends Dialog {
|
|||
|
||||
if (!roomId) return;
|
||||
|
||||
this.roomId = roomId;
|
||||
this._setKeyAndQrCode();
|
||||
|
||||
this._joinPublicRoom(roomId, true);
|
||||
|
@ -2177,29 +2176,36 @@ 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 userJoinedOtherRoom = roomId === this.roomIdJoin;
|
||||
|
||||
if (!peerId || !roomId || (!isInitiatedRoomId && !isJoinedRoomId)) return;
|
||||
if (!roomId || (!peerJoinedThisRoom && !userJoinedOtherRoom)) return;
|
||||
|
||||
this.hide();
|
||||
|
||||
sessionStorage.setItem('public_room_id', roomId);
|
||||
|
||||
if (isJoinedRoomId) {
|
||||
|
||||
if (userJoinedOtherRoom) {
|
||||
// When switching rooms: Disconnect devices in old public room
|
||||
if (this.roomId && roomId !== this.roomId) {
|
||||
Events.fire('leave-public-room', { publicRoomId: this.roomId });
|
||||
}
|
||||
|
||||
this.roomIdJoin = null;
|
||||
this.roomId = roomId;
|
||||
this.roomIdJoin = false;
|
||||
this._setKeyAndQrCode();
|
||||
}
|
||||
}
|
||||
|
@ -2212,7 +2218,7 @@ class PublicRoomDialog extends Dialog {
|
|||
}
|
||||
|
||||
_leavePublicRoom() {
|
||||
Events.fire('leave-public-room', this.roomId);
|
||||
Events.fire('leave-public-room', { publicRoomId: this.roomId });
|
||||
}
|
||||
|
||||
_onPublicRoomLeft() {
|
||||
|
|
|
@ -86,7 +86,7 @@ export default class PairDropWsServer {
|
|||
this._onJoinPublicRoom(sender, message);
|
||||
break;
|
||||
case 'leave-public-room':
|
||||
this._onLeavePublicRoom(sender);
|
||||
this._onLeavePublicRoom(sender, message);
|
||||
break;
|
||||
case 'signal':
|
||||
this._signalAndWsRelay(sender, message);
|
||||
|
@ -251,11 +251,13 @@ export default class PairDropWsServer {
|
|||
return;
|
||||
}
|
||||
|
||||
this._leavePublicRoom(sender);
|
||||
this._leavePublicRoom(sender, true);
|
||||
this._joinPublicRoom(sender, message.publicRoomId);
|
||||
}
|
||||
|
||||
_onLeavePublicRoom(sender) {
|
||||
_onLeavePublicRoom(sender, message) {
|
||||
if (sender.publicRoomId !== message.publicRoomId) return;
|
||||
|
||||
this._leavePublicRoom(sender, true);
|
||||
this._send(sender, { type: 'public-room-left' });
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue