mirror of
https://github.com/schlagmichdoch/PairDrop.git
synced 2025-04-24 16:56:16 -04:00
Squashed commit of the following:
commitc04cdad7db
Author: RobinLinus <robinlinus@users.noreply.github.com> Date: Sat Sep 22 08:47:40 2018 +0200 Cleanup commit891859680a
Author: RobinLinus <robinlinus@users.noreply.github.com> Date: Sat Sep 22 05:55:09 2018 +0200 Refactor about page commit04415ef28f
Author: RobinLinus <robinlinus@users.noreply.github.com> Date: Sat Sep 22 04:44:17 2018 +0200 Cleanup commit52bd7692e9
Author: RobinLinus <robinlinus@users.noreply.github.com> Date: Fri Sep 21 23:19:54 2018 +0200 Notifications Android & Desktop commitf537b96213
Author: RobinLinus <robinlinus@users.noreply.github.com> Date: Fri Sep 21 22:32:39 2018 +0200 Notifications commit476cb0ae65
Author: RobinLinus <robinlinus@users.noreply.github.com> Date: Fri Sep 21 22:31:46 2018 +0200 Notifications commit5a631d3833
Author: RobinLinus <robinlinus@users.noreply.github.com> Date: Fri Sep 21 21:21:44 2018 +0200 Notfication API on android commit600d3551f4
Author: RobinLinus <robinlinus@users.noreply.github.com> Date: Fri Sep 21 21:12:11 2018 +0200 Add will-change commit3ac40fb3d7
Author: RobinLinus <robinlinus@users.noreply.github.com> Date: Fri Sep 21 21:04:48 2018 +0200 Don’t reconnect if already connecting commit9c9ca70d05
Author: RobinLinus <robinlinus@users.noreply.github.com> Date: Fri Sep 21 20:55:15 2018 +0200 Reconnect on rejoin room commit7194c65c74
Author: RobinLinus <robinlinus@users.noreply.github.com> Date: Fri Sep 21 20:51:56 2018 +0200 Reconnect on rejoin room commit0ede41f8d5
Author: RobinLinus <robinlinus@users.noreply.github.com> Date: Fri Sep 21 20:34:49 2018 +0200 Cancel keep alive on join room commit1d9581632f
Author: RobinLinus <robinlinus@users.noreply.github.com> Date: Fri Sep 21 20:25:54 2018 +0200 Cancel keep alive on join room commite71564a97c
Author: RobinLinus <robinlinus@users.noreply.github.com> Date: Fri Sep 21 20:15:55 2018 +0200 Cancel keep alive on join room commit0731a21d68
Author: RobinLinus <robinlinus@users.noreply.github.com> Date: Fri Sep 21 20:09:49 2018 +0200 Cancel keep alive on join room commit61697d3abc
Author: RobinLinus <robinlinus@users.noreply.github.com> Date: Fri Sep 21 20:01:59 2018 +0200 Cancel keep alive on join room commitb0fd89eb96
Merge:4cf2bed
b67afca
Author: RobinLinus <robinlinus@users.noreply.github.com> Date: Fri Sep 21 19:55:32 2018 +0200 Merge remote-tracking branch 'origin/master' into dev # Conflicts: # server/index.js commit4cf2beda90
Author: RobinLinus <robinlinus@users.noreply.github.com> Date: Fri Sep 21 19:49:16 2018 +0200 Fix beforeunload on iphone commit728aabd449
Author: RobinLinus <robinlinus@users.noreply.github.com> Date: Fri Sep 21 19:32:59 2018 +0200 Fix typo in server commit96e37aef40
Author: RobinLinus <robinlinus@users.noreply.github.com> Date: Fri Sep 21 19:25:01 2018 +0200 Fix typo commit31e5f635d1
Author: RobinLinus <robinlinus@users.noreply.github.com> Date: Fri Sep 21 19:24:01 2018 +0200 Add connection state handler commite573d57419
Author: RobinLinus <robinlinus@users.noreply.github.com> Date: Fri Sep 21 19:17:07 2018 +0200 STUN server tests commit6a1de29267
Merge:6317c25
92a5f3b
Author: RobinLinus <robinlinus@users.noreply.github.com> Date: Fri Sep 21 19:15:31 2018 +0200 Merge branch 'dev' of github.com:RobinLinus/snapdrop into dev # Conflicts: # client/scripts/network.js commit6317c25b10
Author: RobinLinus <robinlinus@users.noreply.github.com> Date: Fri Sep 21 19:14:25 2018 +0200 Cleanup; fix STUN servers commit92a5f3b782
Author: RobinLinus <robinlinus@users.noreply.github.com> Date: Fri Sep 21 19:14:25 2018 +0200 Cleanup; fix STUN servers commite9eeea48e5
Author: RobinLinus <robinlinus@users.noreply.github.com> Date: Fri Sep 21 18:54:52 2018 +0200 Fix notifications on android commit36ec13d428
Author: RobinLinus <robinlinus@users.noreply.github.com> Date: Fri Sep 21 18:53:31 2018 +0200 Fix uncaught error in server commitabf96c0228
Author: RobinLinus <robinlinus@users.noreply.github.com> Date: Fri Sep 21 16:36:59 2018 +0200 Test
This commit is contained in:
parent
44bd3edd7b
commit
87a2dec992
5 changed files with 181 additions and 181 deletions
|
@ -2,8 +2,8 @@ class ServerConnection {
|
|||
|
||||
constructor() {
|
||||
this._connect();
|
||||
Events.on('beforeunload', e => this._disconnect(), false);
|
||||
Events.on('pagehide', e => this._disconnect(), false);
|
||||
Events.on('beforeunload', e => this._disconnect());
|
||||
Events.on('pagehide', e => this._disconnect());
|
||||
document.addEventListener('visibilitychange', e => this._onVisibilityChange());
|
||||
|
||||
}
|
||||
|
@ -20,14 +20,6 @@ class ServerConnection {
|
|||
this._socket = ws;
|
||||
}
|
||||
|
||||
_isConnected() {
|
||||
return this._socket && this._socket.readyState === this._socket.OPEN;
|
||||
}
|
||||
|
||||
_isConnecting() {
|
||||
return this._socket && this._socket.readyState === this._socket.CONNECTING;
|
||||
}
|
||||
|
||||
_onMessage(msg) {
|
||||
msg = JSON.parse(msg);
|
||||
console.log('WS:', msg);
|
||||
|
@ -68,6 +60,7 @@ class ServerConnection {
|
|||
|
||||
_disconnect() {
|
||||
this.send({ type: 'disconnect' });
|
||||
this._socket.onclose = null;
|
||||
this._socket.close();
|
||||
}
|
||||
|
||||
|
@ -82,6 +75,14 @@ class ServerConnection {
|
|||
if (document.hidden) return;
|
||||
this._connect();
|
||||
}
|
||||
|
||||
_isConnected() {
|
||||
return this._socket && this._socket.readyState === this._socket.OPEN;
|
||||
}
|
||||
|
||||
_isConnecting() {
|
||||
return this._socket && this._socket.readyState === this._socket.CONNECTING;
|
||||
}
|
||||
}
|
||||
|
||||
class Peer {
|
||||
|
@ -130,7 +131,7 @@ class Peer {
|
|||
}
|
||||
|
||||
_onReceivedPartitionEnd(offset) {
|
||||
this.sendJSON({ type: 'partition_received', offset: offset });
|
||||
this.sendJSON({ type: 'partition-received', offset: offset });
|
||||
}
|
||||
|
||||
_sendNextPartition() {
|
||||
|
@ -156,7 +157,7 @@ class Peer {
|
|||
case 'partition':
|
||||
this._onReceivedPartitionEnd(message);
|
||||
break;
|
||||
case 'partition_received':
|
||||
case 'partition-received':
|
||||
this._sendNextPartition();
|
||||
break;
|
||||
case 'progress':
|
||||
|
@ -192,16 +193,12 @@ class Peer {
|
|||
}
|
||||
|
||||
_onDownloadProgress(progress) {
|
||||
Events.fire('file-progress', {
|
||||
sender: this._peerId,
|
||||
progress: progress
|
||||
});
|
||||
Events.fire('file-progress', { sender: this._peerId, progress: progress });
|
||||
}
|
||||
|
||||
_onFileReceived(proxyFile) {
|
||||
Events.fire('file-received', proxyFile);
|
||||
this.sendJSON({ type: 'transfer-complete' });
|
||||
// this._digester = null;
|
||||
}
|
||||
|
||||
_onTransferCompleted() {
|
||||
|
@ -213,17 +210,13 @@ class Peer {
|
|||
}
|
||||
|
||||
sendText(text) {
|
||||
this.sendJSON({
|
||||
type: 'text',
|
||||
text: btoa(unescape(encodeURIComponent(text)))
|
||||
});
|
||||
const unescaped = btoa(unescape(encodeURIComponent(text)));
|
||||
this.sendJSON({ type: 'text', text: unescaped });
|
||||
}
|
||||
|
||||
_onTextReceived(message) {
|
||||
Events.fire('text-received', {
|
||||
text: decodeURIComponent(escape(atob(message.text))),
|
||||
sender: this._peerId
|
||||
});
|
||||
const escaped = decodeURIComponent(escape(atob(message.text)));
|
||||
Events.fire('text-received', { text: escaped, sender: this._peerId });
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -232,35 +225,37 @@ class RTCPeer extends Peer {
|
|||
constructor(serverConnection, peerId) {
|
||||
super(serverConnection, peerId);
|
||||
if (!peerId) return; // we will listen for a caller
|
||||
this._start(peerId, true);
|
||||
this._connect(peerId, true);
|
||||
}
|
||||
|
||||
_start(peerId, isCaller) {
|
||||
if (!this._peer) {
|
||||
this._isCaller = isCaller;
|
||||
this._peerId = peerId;
|
||||
this._peer = new RTCPeerConnection(RTCPeer.config);
|
||||
this._peer.onicecandidate = e => this._onIceCandidate(e);
|
||||
this._peer.onconnectionstatechange = e => this._onConnectionStateChange(e);
|
||||
}
|
||||
_connect(peerId, isCaller) {
|
||||
if (!this._conn) this._openConnection(peerId, isCaller);
|
||||
|
||||
if (isCaller) {
|
||||
this._createChannel();
|
||||
this._openChannel();
|
||||
} else {
|
||||
this._peer.ondatachannel = e => this._onChannelOpened(e);
|
||||
this._conn.ondatachannel = e => this._onChannelOpened(e);
|
||||
}
|
||||
}
|
||||
|
||||
_createChannel() {
|
||||
const channel = this._peer.createDataChannel('data-channel', { reliable: true });
|
||||
_openConnection(peerId, isCaller) {
|
||||
this._isCaller = isCaller;
|
||||
this._peerId = peerId;
|
||||
this._conn = new RTCPeerConnection(RTCPeer.config);
|
||||
this._conn.onicecandidate = e => this._onIceCandidate(e);
|
||||
this._conn.onconnectionstatechange = e => this._onConnectionStateChange(e);
|
||||
}
|
||||
|
||||
_openChannel() {
|
||||
const channel = this._conn.createDataChannel('data-channel', { reliable: true });
|
||||
channel.binaryType = 'arraybuffer';
|
||||
channel.onopen = e => this._onChannelOpened(e);
|
||||
this._peer.createOffer(d => this._onDescription(d), e => this._onError(e));
|
||||
this._conn.createOffer(d => this._onDescription(d), e => this._onError(e));
|
||||
}
|
||||
|
||||
_onDescription(description) {
|
||||
// description.sdp = description.sdp.replace('b=AS:30', 'b=AS:1638400');
|
||||
this._peer.setLocalDescription(description,
|
||||
this._conn.setLocalDescription(description,
|
||||
_ => this._sendSignal({ sdp: description }),
|
||||
e => this._onError(e));
|
||||
}
|
||||
|
@ -270,23 +265,16 @@ class RTCPeer extends Peer {
|
|||
this._sendSignal({ ice: event.candidate });
|
||||
}
|
||||
|
||||
_sendSignal(signal) {
|
||||
signal.type = 'signal';
|
||||
signal.to = this._peerId;
|
||||
this._server.send(signal);
|
||||
}
|
||||
|
||||
onServerMessage(message) {
|
||||
if (!this._peer) this._start(message.sender, false);
|
||||
const conn = this._peer;
|
||||
if (!this._conn) this._connect(message.sender, false);
|
||||
|
||||
if (message.sdp) {
|
||||
this._peer.setRemoteDescription(new RTCSessionDescription(message.sdp), () => {
|
||||
this._conn.setRemoteDescription(new RTCSessionDescription(message.sdp), () => {
|
||||
if (message.sdp.type !== 'offer') return;
|
||||
this._peer.createAnswer(d => this._onDescription(d), e => this._onError(e));
|
||||
this._conn.createAnswer(d => this._onDescription(d), e => this._onError(e));
|
||||
}, e => this._onError(e));
|
||||
} else if (message.ice) {
|
||||
this._peer.addIceCandidate(new RTCIceCandidate(message.ice));
|
||||
this._conn.addIceCandidate(new RTCIceCandidate(message.ice));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -301,34 +289,48 @@ class RTCPeer extends Peer {
|
|||
_onChannelClosed() {
|
||||
console.log('RTC: channel closed', this._peerId);
|
||||
if (!this.isCaller) return;
|
||||
this._start(this._peerId, true); // reopen the channel
|
||||
this._connect(this._peerId, true); // reopen the channel
|
||||
}
|
||||
|
||||
_onConnectionStateChange(e) {
|
||||
console.log('RTC: state changed:', this._peer.connectionState);
|
||||
switch (this._peer.connectionState) {
|
||||
console.log('RTC: state changed:', this._conn.connectionState);
|
||||
switch (this._conn.connectionState) {
|
||||
case 'disconnected':
|
||||
this._onChannelClosed();
|
||||
break;
|
||||
case 'failed':
|
||||
this._peer = null;
|
||||
this._conn = null;
|
||||
this._onChannelClosed();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
_send(message) {
|
||||
this._channel.send(message);
|
||||
}
|
||||
|
||||
_onError(error) {
|
||||
console.error(error);
|
||||
}
|
||||
|
||||
_send(message) {
|
||||
this._channel.send(message);
|
||||
}
|
||||
|
||||
_sendSignal(signal) {
|
||||
signal.type = 'signal';
|
||||
signal.to = this._peerId;
|
||||
this._server.send(signal);
|
||||
}
|
||||
|
||||
refresh() {
|
||||
// check if channel open. otherwise create one
|
||||
if (this._peer && this._channel && this._channel.readyState !== 'open') return;
|
||||
this._start(this._peerId, this._isCaller);
|
||||
// check if channel is open. otherwise create one
|
||||
if (this._isConnected() || this._isConnecting()) return;
|
||||
this._connect(this._peerId, this._isCaller);
|
||||
}
|
||||
|
||||
_isConnected() {
|
||||
return this._channel && this._channel.readyState === 'open';
|
||||
}
|
||||
|
||||
_isConnecting() {
|
||||
return this._channel && this._channel.readyState === 'connecting';
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -422,7 +424,7 @@ class FileChunker {
|
|||
this._partitionSize += chunk.byteLength;
|
||||
this._onChunk(chunk);
|
||||
if (this._isPartitionEnd() || this.isFileEnd()) {
|
||||
this._onPartitionEnd(this._partitionSize);
|
||||
this._onPartitionEnd(this._offset);
|
||||
return;
|
||||
}
|
||||
this._readChunk();
|
||||
|
@ -447,6 +449,7 @@ class FileChunker {
|
|||
}
|
||||
|
||||
class FileDigester {
|
||||
|
||||
constructor(meta, callback) {
|
||||
this._buffer = [];
|
||||
this._bytesReceived = 0;
|
||||
|
@ -462,8 +465,8 @@ class FileDigester {
|
|||
const totalChunks = this._buffer.length;
|
||||
this.progress = this._bytesReceived / this._size;
|
||||
if (this._bytesReceived < this._size) return;
|
||||
|
||||
let received = new Blob(this._buffer, { type: this._mime }); // pass a useful mime type here
|
||||
// we are done
|
||||
let received = new Blob(this._buffer, { type: this._mime });
|
||||
let url = URL.createObjectURL(received);
|
||||
this._callback({
|
||||
name: this._name,
|
||||
|
@ -471,8 +474,8 @@ class FileDigester {
|
|||
size: this._size,
|
||||
url: url
|
||||
});
|
||||
this._callback = null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class Events {
|
||||
|
@ -485,7 +488,6 @@ class Events {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
window.isRtcSupported = !!(window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection);
|
||||
|
||||
RTCPeer.config = {
|
||||
|
|
|
@ -392,8 +392,8 @@ class Notifications {
|
|||
}
|
||||
|
||||
_copyText(message, notification) {
|
||||
document.copy(message);
|
||||
notification.close();
|
||||
if(!document.copy(message)) return;
|
||||
this._notify('Copied text to clipboard');
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue