merge master into branch

This commit is contained in:
schlagmichdoch 2023-03-03 18:01:24 +01:00
commit c0a4224a59
13 changed files with 587 additions and 493 deletions

View file

@ -34,6 +34,7 @@ class ServerConnection {
_onOpen() {
console.log('WS: server connected');
Events.fire('ws-connected');
if (this._isReconnect) Events.fire('notify-user', 'Connected.');
}
_sendRoomSecrets(roomSecrets) {
@ -162,15 +163,17 @@ class ServerConnection {
this._socket.close();
this._socket = null;
Events.fire('ws-disconnected');
this._isReconnect = true;
}
}
_onDisconnect() {
console.log('WS: server disconnected');
Events.fire('notify-user', 'No server connection. Retry in 5s...');
Events.fire('notify-user', 'Connecting..');
clearTimeout(this._reconnectTimer);
this._reconnectTimer = setTimeout(_ => this._connect(), 5000);
Events.fire('ws-disconnected');
this._isReconnect = true;
}
_onVisibilityChange() {
@ -331,31 +334,30 @@ class Peer {
this.sendJSON({ type: 'progress', progress: progress });
}
_onMessage(message, logMessage = true) {
_onMessage(message) {
if (typeof message !== 'string') {
this._onChunkReceived(message);
return;
}
message = JSON.parse(message);
if (logMessage) console.log('RTC:', message);
switch (message.type) {
const messageJSON = JSON.parse(message);
switch (messageJSON.type) {
case 'request':
this._onFilesTransferRequest(message);
this._onFilesTransferRequest(messageJSON);
break;
case 'header':
this._onFilesHeader(message);
this._onFilesHeader(messageJSON);
break;
case 'partition':
this._onReceivedPartitionEnd(message);
this._onReceivedPartitionEnd(messageJSON);
break;
case 'partition-received':
this._sendNextPartition();
break;
case 'progress':
this._onDownloadProgress(message.progress);
this._onDownloadProgress(messageJSON.progress);
break;
case 'files-transfer-response':
this._onFileTransferRequestResponded(message);
this._onFileTransferRequestResponded(messageJSON);
break;
case 'file-transfer-complete':
this._onFileTransferCompleted();
@ -364,7 +366,7 @@ class Peer {
this._onMessageTransferCompleted();
break;
case 'text':
this._onTextReceived(message);
this._onTextReceived(messageJSON);
break;
}
}
@ -458,7 +460,7 @@ class Peer {
if (!this._requestAccepted.header.length) {
this._busy = false;
Events.fire('set-progress', {peerId: this._peerId, progress: 0, status: 'process'});
Events.fire('files-received', {sender: this._peerId, files: this._filesReceived, request: this._requestAccepted});
Events.fire('files-received', {sender: this._peerId, files: this._filesReceived, imagesOnly: this._requestAccepted.imagesOnly, totalSize: this._requestAccepted.totalSize});
this._filesReceived = [];
this._requestAccepted = null;
}
@ -509,6 +511,7 @@ class RTCPeer extends Peer {
constructor(serverConnection, peerId, roomType, roomSecret) {
super(serverConnection, peerId, roomType, roomSecret);
this.rtcSupported = true;
if (!peerId) return; // we will listen for a caller
this._connect(peerId, true);
}
@ -585,6 +588,13 @@ class RTCPeer extends Peer {
this._channel = channel;
}
_onMessage(message) {
if (typeof message === 'string') {
console.log('RTC:', JSON.parse(message));
}
super._onMessage(message);
}
getConnectionHash() {
const localDescriptionLines = this._conn.localDescription.sdp.split("\r\n");
const remoteDescriptionLines = this._conn.remoteDescription.sdp.split("\r\n");
@ -689,6 +699,7 @@ class WSPeer extends Peer {
constructor(serverConnection, peerId, roomType, roomSecret) {
super(serverConnection, peerId, roomType, roomSecret);
this.rtcSupported = false;
if (!peerId) return; // we will listen for a caller
this._sendSignal();
}
@ -707,15 +718,15 @@ class WSPeer extends Peer {
this._server.send(message);
}
_sendSignal() {
this.sendJSON({type: 'signal'});
_sendSignal(connected = false) {
this.sendJSON({type: 'signal', connected: connected});
}
onServerMessage(message) {
Events.fire('peer-connected', {peerId: message.sender.id, connectionHash: this.getConnectionHash()})
if (this._peerId) return;
if (message.connected) return;
this._peerId = message.sender.id;
this._sendSignal();
this._sendSignal(true);
}
getConnectionHash() {
@ -737,6 +748,7 @@ class PeersManager {
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));
Events.on('ws-disconnected', _ => this._onWsDisconnected());
Events.on('ws-relay', e => this._onWsRelay(e.detail));
}
@ -756,7 +768,7 @@ class PeersManager {
_onWsRelay(message) {
const messageJSON = JSON.parse(message)
if (messageJSON.type === 'ws-chunk') message = base64ToArrayBuffer(messageJSON.chunk);
this.peers[messageJSON.sender.id]._onMessage(message, false)
this.peers[messageJSON.sender.id]._onMessage(message)
}
_onPeers(msg) {
@ -804,15 +816,24 @@ class PeersManager {
}
_onPeerLeft(msg) {
if (this.peers[msg.peerId] && !this.peers[msg.peerId].rtcSupported) {
console.log('WSPeer left:', msg.peerId)
Events.fire('peer-disconnected', msg.peerId)
if (this.peers[msg.peerId] && (!this.peers[msg.peerId].rtcSupported || !window.isRtcSupported)) {
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);
}
}
_onWsDisconnected() {
for (const peerId in this.peers) {
console.debug(this.peers[peerId].rtcSupported);
if (this.peers[peerId] && (!this.peers[peerId].rtcSupported || !window.isRtcSupported)) {
Events.fire('peer-disconnected', peerId);
}
}
}
_onPeerDisconnected(peerId) {
const peer = this.peers[peerId];
delete this.peers[peerId];