mirror of
https://github.com/schlagmichdoch/PairDrop.git
synced 2025-04-21 15:26:17 -04:00
rolled back some changes to stabilize WebRTC connections
This commit is contained in:
parent
616f6a6799
commit
1eba7359d1
5 changed files with 61 additions and 38 deletions
3
index.js
3
index.js
|
@ -105,7 +105,6 @@ class PairDropServer {
|
||||||
this._joinRoom(peer);
|
this._joinRoom(peer);
|
||||||
peer.socket.on('message', message => this._onMessage(peer, message));
|
peer.socket.on('message', message => this._onMessage(peer, message));
|
||||||
peer.socket.onerror = e => console.error(e);
|
peer.socket.onerror = e => console.error(e);
|
||||||
peer.socket.onclose = _ => this._onDisconnect(peer);
|
|
||||||
this._keepAlive(peer);
|
this._keepAlive(peer);
|
||||||
|
|
||||||
// send displayName
|
// send displayName
|
||||||
|
@ -409,7 +408,7 @@ class PairDropServer {
|
||||||
|
|
||||||
_keepAlive(peer) {
|
_keepAlive(peer) {
|
||||||
this._cancelKeepAlive(peer);
|
this._cancelKeepAlive(peer);
|
||||||
let timeout = 30000;
|
let timeout = 500;
|
||||||
if (!peer.lastBeat) {
|
if (!peer.lastBeat) {
|
||||||
peer.lastBeat = Date.now();
|
peer.lastBeat = Date.now();
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,6 +7,7 @@ class ServerConnection {
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this._connect();
|
this._connect();
|
||||||
|
Events.on('beforeunload', _ => this._disconnect());
|
||||||
Events.on('pagehide', _ => this._disconnect());
|
Events.on('pagehide', _ => this._disconnect());
|
||||||
document.addEventListener('visibilitychange', _ => this._onVisibilityChange());
|
document.addEventListener('visibilitychange', _ => this._onVisibilityChange());
|
||||||
if (navigator.connection) navigator.connection.addEventListener('change', _ => this._reconnect());
|
if (navigator.connection) navigator.connection.addEventListener('change', _ => this._reconnect());
|
||||||
|
@ -516,11 +517,13 @@ class RTCPeer extends Peer {
|
||||||
}
|
}
|
||||||
|
|
||||||
_openChannel() {
|
_openChannel() {
|
||||||
|
if (!this._conn) return;
|
||||||
const channel = this._conn.createDataChannel('data-channel', {
|
const channel = this._conn.createDataChannel('data-channel', {
|
||||||
ordered: true,
|
ordered: true,
|
||||||
reliable: true // Obsolete. See https://developer.mozilla.org/en-US/docs/Web/API/RTCDataChannel/reliable
|
reliable: true // Obsolete. See https://developer.mozilla.org/en-US/docs/Web/API/RTCDataChannel/reliable
|
||||||
});
|
});
|
||||||
channel.onopen = e => this._onChannelOpened(e);
|
channel.onopen = e => this._onChannelOpened(e);
|
||||||
|
channel.onerror = e => this._onError(e);
|
||||||
this._conn.createOffer().then(d => this._onDescription(d)).catch(e => this._onError(e));
|
this._conn.createOffer().then(d => this._onDescription(d)).catch(e => this._onError(e));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -542,12 +545,15 @@ class RTCPeer extends Peer {
|
||||||
if (message.sdp) {
|
if (message.sdp) {
|
||||||
this._conn.setRemoteDescription(message.sdp)
|
this._conn.setRemoteDescription(message.sdp)
|
||||||
.then( _ => {
|
.then( _ => {
|
||||||
return this._conn.createAnswer()
|
if (message.sdp.type === 'offer') {
|
||||||
.then(d => this._onDescription(d));
|
return this._conn.createAnswer()
|
||||||
|
.then(d => this._onDescription(d));
|
||||||
|
}
|
||||||
})
|
})
|
||||||
.catch(e => this._onError(e));
|
.catch(e => this._onError(e));
|
||||||
} else if (message.ice) {
|
} else if (message.ice) {
|
||||||
this._conn.addIceCandidate(new RTCIceCandidate(message.ice));
|
this._conn.addIceCandidate(new RTCIceCandidate(message.ice))
|
||||||
|
.catch(e => this._onError(e));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -558,15 +564,27 @@ class RTCPeer extends Peer {
|
||||||
channel.binaryType = 'arraybuffer';
|
channel.binaryType = 'arraybuffer';
|
||||||
channel.onmessage = e => this._onMessage(e.data);
|
channel.onmessage = e => this._onMessage(e.data);
|
||||||
channel.onclose = _ => this._onChannelClosed();
|
channel.onclose = _ => this._onChannelClosed();
|
||||||
Events.on('pagehide', _ => this._conn.close());
|
Events.on('beforeunload', e => this._onBeforeUnload(e));
|
||||||
|
Events.on('pagehide', _ => this._closeChannel());
|
||||||
this._channel = channel;
|
this._channel = channel;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_onBeforeUnload(e) {
|
||||||
|
if (this._busy) {
|
||||||
|
e.preventDefault();
|
||||||
|
return "There are unfinished transfers. Are you sure you want to close?";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
_closeChannel() {
|
||||||
|
if (this._channel) this._channel.onclose = null;
|
||||||
|
if (this._conn) this._conn.close();
|
||||||
|
this._conn = null;
|
||||||
|
}
|
||||||
|
|
||||||
_onChannelClosed() {
|
_onChannelClosed() {
|
||||||
console.log('RTC: channel closed', this._peerId);
|
console.log('RTC: channel closed', this._peerId);
|
||||||
Events.fire('peer-disconnected', this._peerId);
|
Events.fire('peer-disconnected', this._peerId);
|
||||||
if (this._channel) this._channel.onclose = null;
|
|
||||||
this._conn.close();
|
|
||||||
if (!this._isCaller) return;
|
if (!this._isCaller) return;
|
||||||
this._connect(this._peerId, true); // reopen the channel
|
this._connect(this._peerId, true); // reopen the channel
|
||||||
}
|
}
|
||||||
|
@ -637,16 +655,6 @@ class PeersManager {
|
||||||
Events.on('send-text', e => this._onSendText(e.detail));
|
Events.on('send-text', e => this._onSendText(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));
|
||||||
Events.on('beforeunload', e => this._onBeforeUnload(e));
|
|
||||||
}
|
|
||||||
|
|
||||||
_onBeforeUnload(e) {
|
|
||||||
for (const peerId in this.peers) {
|
|
||||||
if (this.peers[peerId]._busy) {
|
|
||||||
e.preventDefault();
|
|
||||||
return "There are unfinished transfers. Are you sure you want to close?";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_onMessage(message) {
|
_onMessage(message) {
|
||||||
|
@ -701,13 +709,17 @@ class PeersManager {
|
||||||
_onPeerDisconnected(peerId) {
|
_onPeerDisconnected(peerId) {
|
||||||
const peer = this.peers[peerId];
|
const peer = this.peers[peerId];
|
||||||
delete this.peers[peerId];
|
delete this.peers[peerId];
|
||||||
|
if (!peer || !peer._conn) return;
|
||||||
|
if (peer._channel) peer._channel.onclose = null;
|
||||||
|
peer._conn.close();
|
||||||
|
peer._busy = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
_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];
|
||||||
if (peer._roomSecret === roomSecret) {
|
if (peer._roomSecret === roomSecret) {
|
||||||
this._onPeerLeft(peerId);
|
this._onPeerDisconnected(peerId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
const cacheVersion = 'v9';
|
const cacheVersion = 'v10';
|
||||||
const cacheTitle = `pairdrop-cache-${cacheVersion}`;
|
const cacheTitle = `pairdrop-cache-${cacheVersion}`;
|
||||||
const urlsToCache = [
|
const urlsToCache = [
|
||||||
'index.html',
|
'index.html',
|
||||||
|
|
|
@ -5,6 +5,7 @@ class ServerConnection {
|
||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this._connect();
|
this._connect();
|
||||||
|
Events.on('beforeunload', _ => this._disconnect());
|
||||||
Events.on('pagehide', _ => this._disconnect());
|
Events.on('pagehide', _ => this._disconnect());
|
||||||
document.addEventListener('visibilitychange', _ => this._onVisibilityChange());
|
document.addEventListener('visibilitychange', _ => this._onVisibilityChange());
|
||||||
if (navigator.connection) navigator.connection.addEventListener('change', _ => this._reconnect());
|
if (navigator.connection) navigator.connection.addEventListener('change', _ => this._reconnect());
|
||||||
|
@ -526,11 +527,13 @@ class RTCPeer extends Peer {
|
||||||
}
|
}
|
||||||
|
|
||||||
_openChannel() {
|
_openChannel() {
|
||||||
|
if (!this._conn) return;
|
||||||
const channel = this._conn.createDataChannel('data-channel', {
|
const channel = this._conn.createDataChannel('data-channel', {
|
||||||
ordered: true,
|
ordered: true,
|
||||||
reliable: true // Obsolete. See https://developer.mozilla.org/en-US/docs/Web/API/RTCDataChannel/reliable
|
reliable: true // Obsolete. See https://developer.mozilla.org/en-US/docs/Web/API/RTCDataChannel/reliable
|
||||||
});
|
});
|
||||||
channel.onopen = e => this._onChannelOpened(e);
|
channel.onopen = e => this._onChannelOpened(e);
|
||||||
|
channel.onerror = e => this._onError(e);
|
||||||
this._conn.createOffer().then(d => this._onDescription(d)).catch(e => this._onError(e));
|
this._conn.createOffer().then(d => this._onDescription(d)).catch(e => this._onError(e));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -552,12 +555,15 @@ class RTCPeer extends Peer {
|
||||||
if (message.sdp) {
|
if (message.sdp) {
|
||||||
this._conn.setRemoteDescription(message.sdp)
|
this._conn.setRemoteDescription(message.sdp)
|
||||||
.then( _ => {
|
.then( _ => {
|
||||||
return this._conn.createAnswer()
|
if (message.sdp.type === 'offer') {
|
||||||
.then(d => this._onDescription(d));
|
return this._conn.createAnswer()
|
||||||
|
.then(d => this._onDescription(d));
|
||||||
|
}
|
||||||
})
|
})
|
||||||
.catch(e => this._onError(e));
|
.catch(e => this._onError(e));
|
||||||
} else if (message.ice) {
|
} else if (message.ice) {
|
||||||
this._conn.addIceCandidate(new RTCIceCandidate(message.ice));
|
this._conn.addIceCandidate(new RTCIceCandidate(message.ice))
|
||||||
|
.catch(e => this._onError(e));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -568,15 +574,27 @@ class RTCPeer extends Peer {
|
||||||
channel.binaryType = 'arraybuffer';
|
channel.binaryType = 'arraybuffer';
|
||||||
channel.onmessage = e => this._onMessage(e.data);
|
channel.onmessage = e => this._onMessage(e.data);
|
||||||
channel.onclose = _ => this._onChannelClosed();
|
channel.onclose = _ => this._onChannelClosed();
|
||||||
Events.on('pagehide', _ => this._conn.close());
|
Events.on('beforeunload', e => this._onBeforeUnload(e));
|
||||||
|
Events.on('pagehide', _ => this._closeChannel());
|
||||||
this._channel = channel;
|
this._channel = channel;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
_onBeforeUnload(e) {
|
||||||
|
if (this._busy) {
|
||||||
|
e.preventDefault();
|
||||||
|
return "There are unfinished transfers. Are you sure you want to close?";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
_closeChannel() {
|
||||||
|
if (this._channel) this._channel.onclose = null;
|
||||||
|
if (this._conn) this._conn.close();
|
||||||
|
this._conn = null;
|
||||||
|
}
|
||||||
|
|
||||||
_onChannelClosed() {
|
_onChannelClosed() {
|
||||||
console.log('RTC: channel closed', this._peerId);
|
console.log('RTC: channel closed', this._peerId);
|
||||||
Events.fire('peer-disconnected', this._peerId);
|
Events.fire('peer-disconnected', this._peerId);
|
||||||
if (this._channel) this._channel.onclose = null;
|
|
||||||
this._conn.close();
|
|
||||||
if (!this._isCaller) return;
|
if (!this._isCaller) return;
|
||||||
this._connect(this._peerId, true); // reopen the channel
|
this._connect(this._peerId, true); // reopen the channel
|
||||||
}
|
}
|
||||||
|
@ -682,19 +700,9 @@ class PeersManager {
|
||||||
Events.on('peer-left', e => this._onPeerLeft(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));
|
||||||
Events.on('beforeunload', e => this._onBeforeUnload(e));
|
|
||||||
Events.on('ws-relay', e => this._onWsRelay(e.detail));
|
Events.on('ws-relay', e => this._onWsRelay(e.detail));
|
||||||
}
|
}
|
||||||
|
|
||||||
_onBeforeUnload(e) {
|
|
||||||
for (const peerId in this.peers) {
|
|
||||||
if (this.peers[peerId]._busy) {
|
|
||||||
e.preventDefault();
|
|
||||||
return "There are unfinished transfers. Are you sure you want to close?";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
_onMessage(message) {
|
_onMessage(message) {
|
||||||
// if different roomType -> abort
|
// if different roomType -> abort
|
||||||
if (this.peers[message.sender.id] && this.peers[message.sender.id]._roomType !== message.roomType) return;
|
if (this.peers[message.sender.id] && this.peers[message.sender.id]._roomType !== message.roomType) return;
|
||||||
|
@ -761,6 +769,10 @@ class PeersManager {
|
||||||
_onPeerDisconnected(peerId) {
|
_onPeerDisconnected(peerId) {
|
||||||
const peer = this.peers[peerId];
|
const peer = this.peers[peerId];
|
||||||
delete this.peers[peerId];
|
delete this.peers[peerId];
|
||||||
|
if (!peer || !peer._conn) return;
|
||||||
|
if (peer._channel) peer._channel.onclose = null;
|
||||||
|
peer._conn.close();
|
||||||
|
peer._busy = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
_onPeerLeft(peerId) {
|
_onPeerLeft(peerId) {
|
||||||
|
@ -774,7 +786,7 @@ class PeersManager {
|
||||||
for (const peerId in this.peers) {
|
for (const peerId in this.peers) {
|
||||||
const peer = this.peers[peerId];
|
const peer = this.peers[peerId];
|
||||||
if (peer._roomSecret === roomSecret) {
|
if (peer._roomSecret === roomSecret) {
|
||||||
this._onPeerLeft(peerId);
|
this._onPeerDisconnected(peerId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
const cacheVersion = 'v9';
|
const cacheVersion = 'v10';
|
||||||
const cacheTitle = `pairdrop-included-ws-fallback-cache-${cacheVersion}`;
|
const cacheTitle = `pairdrop-included-ws-fallback-cache-${cacheVersion}`;
|
||||||
const urlsToCache = [
|
const urlsToCache = [
|
||||||
'index.html',
|
'index.html',
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue