commit e9e8d6336bdba0d17465154ac48578ecad2958d8
parent ab52a58fc54c9a8579524e60b54dc91797b529e2
Author: RobinLinus <robinlinus@users.noreply.github.com>
Date: Tue, 9 Oct 2018 15:45:07 +0200
Refactor and cleanup
Diffstat:
3 files changed, 58 insertions(+), 54 deletions(-)
diff --git a/client/scripts/network.js b/client/scripts/network.js
@@ -1,3 +1,6 @@
+window.URL = window.URL || window.webkitURL;
+window.isRtcSupported = !!(window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection);
+
class ServerConnection {
constructor() {
@@ -5,7 +8,6 @@ class ServerConnection {
Events.on('beforeunload', e => this._disconnect());
Events.on('pagehide', e => this._disconnect());
document.addEventListener('visibilitychange', e => this._onVisibilityChange());
-
}
_connect() {
@@ -40,12 +42,12 @@ class ServerConnection {
this.send({ type: 'pong' });
break;
default:
- console.error('WS: unkown message type', msg)
+ console.error('WS: unkown message type', msg);
}
}
send(message) {
- if (this._socket.readyState !== this._socket.OPEN) return;
+ if (!this._isConnected()) return;
this._socket.send(JSON.stringify(message));
}
@@ -118,7 +120,7 @@ class Peer {
type: 'header',
name: file.name,
mime: file.type,
- size: file.size,
+ size: file.size
});
this._chunker = new FileChunker(file,
chunk => this._send(chunk),
@@ -409,8 +411,8 @@ class WSPeer {
class FileChunker {
constructor(file, onChunk, onPartitionEnd) {
- this._chunkSize = 64000;
- this._maxPartitionSize = 1e6;
+ this._chunkSize = 64000; // 64 KB
+ this._maxPartitionSize = 1e6; // 1 MB
this._offset = 0;
this._partitionSize = 0;
this._file = file;
@@ -499,7 +501,6 @@ class Events {
}
}
-window.isRtcSupported = !!(window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection);
RTCPeer.config = {
'iceServers': [{
diff --git a/client/scripts/ui.js b/client/scripts/ui.js
@@ -25,16 +25,16 @@ class PeersUI {
}
_onPeerLeft(peerId) {
- const peer = $(peerId);
- if (!peer) return;
- peer.remove();
+ const $peer = $(peerId);
+ if (!$peer) return;
+ $peer.remove();
}
_onFileProgress(progress) {
const peerId = progress.sender || progress.recipient;
- const peer = $(peerId);
- if (!peer) return;
- peer.ui.setProgress(progress.progress);
+ const $peer = $(peerId);
+ if (!$peer) return;
+ $peer.ui.setProgress(progress.progress);
}
_clearPeers() {
diff --git a/server/index.js b/server/index.js
@@ -4,9 +4,7 @@ class SnapdropServer {
constructor(port) {
const WebSocket = require('ws');
- this._wss = new WebSocket.Server({
- port: port
- });
+ this._wss = new WebSocket.Server({ port: port });
this._wss.on('connection', (socket, request) => this._onConnection(new Peer(socket, request)));
this._wss.on('headers', (headers, response) => this._onHeaders(headers, response));
@@ -57,7 +55,6 @@ class SnapdropServer {
this._rooms[peer.ip] = {};
}
- // console.log(peer.id, ' joined the room', peer.ip);
// notify all other peers
for (const otherPeerId in this._rooms[peer.ip]) {
const otherPeer = this._rooms[peer.ip][otherPeerId];
@@ -97,10 +94,7 @@ class SnapdropServer {
// notify all other peers
for (const otherPeerId in this._rooms[peer.ip]) {
const otherPeer = this._rooms[peer.ip][otherPeerId];
- this._send(otherPeer, {
- type: 'peer-left',
- peerId: peer.id
- });
+ this._send(otherPeer, { type: 'peer-left', peerId: peer.id });
}
}
}
@@ -109,7 +103,7 @@ class SnapdropServer {
if (!peer) return console.error('undefined peer');
if (this._wss.readyState !== this._wss.OPEN) return console.error('Socket is closed');
message = JSON.stringify(message);
- peer.socket.send(message, error => console.log(error));
+ peer.socket.send(message, error => error ? console.log(error): '');
}
_keepAlive(peer) {
@@ -145,26 +139,57 @@ class Peer {
// set remote ip
- if (request.headers['x-forwarded-for'])
- this.ip = request.headers['x-forwarded-for'].split(/\s*,\s*/)[0];
- else
- this.ip = request.connection.remoteAddress;
+ this._setIP(request);
- if (request.peerId) {
- this.id = request.peerId;
- } else {
- this.id = request.headers.cookie.replace('peerid=', '');
- }
// set peer id
+ this._setPeerId(request)
// is WebRTC supported ?
this.rtcSupported = request.url.indexOf('webrtc') > -1;
// set name
- this.setName(request);
+ this._setName(request);
// for keepalive
this.timerId = 0;
this.lastBeat = Date.now();
}
+ _setIP(request) {
+ if (request.headers['x-forwarded-for']) {
+ this.ip = request.headers['x-forwarded-for'].split(/\s*,\s*/)[0];
+ } else {
+ this.ip = request.connection.remoteAddress;
+ }
+ }
+
+ _setPeerId(request) {
+ if (request.peerId) {
+ this.id = request.peerId;
+ } else {
+ this.id = request.headers.cookie.replace('peerid=', '');
+ }
+ }
+
+ toString() {
+ return `<Peer id=${this.id} ip=${this.ip} rtcSupported=${this.rtcSupported}>`
+ }
+
+ _setName(req) {
+ var ua = parser(req.headers['user-agent']);
+ this.name = {
+ model: ua.device.model,
+ os: ua.os.name,
+ browser: ua.browser.name,
+ type: ua.device.type
+ };
+ }
+
+ getInfo() {
+ return {
+ id: this.id,
+ name: this.name,
+ rtcSupported: this.rtcSupported
+ }
+ }
+
// return uuid of form xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx
static uuid() {
let uuid = '',
@@ -190,28 +215,6 @@ class Peer {
}
return uuid;
};
-
- toString() {
- return `<Peer id=${this.id} ip=${this.ip} rtcSupported=${this.rtcSupported}>`
- }
-
- setName(req) {
- var ua = parser(req.headers['user-agent']);
- this.name = {
- model: ua.device.model,
- os: ua.os.name,
- browser: ua.browser.name,
- type: ua.device.type
- };
- }
-
- getInfo() {
- return {
- id: this.id,
- name: this.name,
- rtcSupported: this.rtcSupported
- }
- }
}
const server = new SnapdropServer(process.env.PORT || 3000);
\ No newline at end of file