2015-12-23 13:57:13 +01:00
|
|
|
<script src="../../bower_components/peerjs/peer.min.js"></script>
|
|
|
|
<link rel="import" href="file-transfer-protocol.html">
|
2015-12-18 17:43:46 +01:00
|
|
|
<dom-module id="p2p-network">
|
|
|
|
<template>
|
|
|
|
</template>
|
|
|
|
<script>
|
|
|
|
'use strict';
|
|
|
|
Polymer({
|
|
|
|
is: 'p2p-network',
|
|
|
|
properties: {
|
|
|
|
me: {
|
2015-12-26 13:33:16 +01:00
|
|
|
type: String
|
2015-12-18 17:43:46 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
attached: function() {
|
|
|
|
this._connectedPeers = {};
|
|
|
|
this._initCallbacks = [];
|
|
|
|
this._unsendMsgs = {};
|
|
|
|
window.onunload = window.onbeforeunload = function() {
|
|
|
|
if (!!this._peer && !this._peer.destroyed) {
|
|
|
|
this._peer.destroy();
|
|
|
|
}
|
|
|
|
}.bind(this);
|
|
|
|
},
|
2015-12-26 13:33:16 +01:00
|
|
|
initialize: function() {
|
2015-12-31 00:42:53 +01:00
|
|
|
clearInterval(this.reconnectTimer);
|
|
|
|
this.reconnectTimer = undefined;
|
|
|
|
var options;
|
|
|
|
if (window.debug) {
|
|
|
|
options = {
|
|
|
|
host: window.location.hostname,
|
|
|
|
port: 3002,
|
|
|
|
path: 'peerjs'
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
options = {
|
|
|
|
host: 'snapdrop.net',
|
|
|
|
port: 443,
|
|
|
|
path: 'peerjs',
|
|
|
|
secure: true
|
|
|
|
};
|
2015-12-30 18:07:37 +01:00
|
|
|
}
|
2015-12-31 00:42:53 +01:00
|
|
|
this._peer = new Peer(this.me, options);
|
|
|
|
this._peer.on('open', function(id) {
|
|
|
|
console.log('My peer ID is: ' + id);
|
|
|
|
this.set('me', id);
|
|
|
|
this._peerOpen = true;
|
|
|
|
this._initCallbacks.forEach(function(cb) {
|
|
|
|
cb();
|
|
|
|
});
|
|
|
|
}.bind(this));
|
|
|
|
|
|
|
|
this._peer.on('connection', this.connect.bind(this));
|
|
|
|
this._peer.on('error', function(err) {
|
|
|
|
console.error(err);
|
|
|
|
//ugly hack to find out error type
|
|
|
|
if (err.message.indexOf('Could not connect to peer') > -1) {
|
|
|
|
delete this._connectedPeers[this.peer];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (err.message.indexOf('Lost connection to server') > -1) {
|
|
|
|
this._peer.destroy();
|
|
|
|
this._reconnect();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}.bind(this));
|
2015-12-18 17:43:46 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
connect: function(c) {
|
|
|
|
var peer = c.peer;
|
|
|
|
|
|
|
|
if (c.label === 'file') {
|
|
|
|
c.on('data', function(data) {
|
2015-12-23 13:57:13 +01:00
|
|
|
console.log(data);
|
|
|
|
var dataView = new Uint8Array(data.file);
|
|
|
|
var dataBlob = new Blob([dataView]);
|
2015-12-18 17:43:46 +01:00
|
|
|
this.fire('file-received', {
|
2015-12-23 13:57:13 +01:00
|
|
|
from: peer,
|
|
|
|
blob: dataBlob,
|
2015-12-18 17:43:46 +01:00
|
|
|
name: data.name,
|
|
|
|
});
|
2015-12-23 13:57:13 +01:00
|
|
|
|
|
|
|
}.bind(this));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (c.label === 'system') {
|
|
|
|
c.on('data', function(data) {
|
|
|
|
data.from = peer;
|
|
|
|
this.fire('system-event', data);
|
2015-12-18 17:43:46 +01:00
|
|
|
}.bind(this));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
connectToPeer: (function() {
|
2015-12-19 01:18:02 +01:00
|
|
|
function request(requestedPeer, callback) {
|
2015-12-18 17:43:46 +01:00
|
|
|
return function() {
|
|
|
|
|
2015-12-23 13:57:13 +01:00
|
|
|
//system messages channel
|
|
|
|
var s = this._peer.connect(requestedPeer, {
|
|
|
|
label: 'system'
|
|
|
|
});
|
|
|
|
|
|
|
|
s.on('open', function() {
|
|
|
|
this.connect(s);
|
|
|
|
if (callback) {
|
|
|
|
callback();
|
|
|
|
}
|
|
|
|
}.bind(this));
|
|
|
|
s.on('error', function(err) {
|
|
|
|
console.log(err);
|
|
|
|
if (err.message.indexOf('Connection is not open') > -1) {
|
2016-01-02 14:23:51 +01:00
|
|
|
console.error('Handle this error!!', err);
|
2015-12-23 13:57:13 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
//files channel
|
2015-12-18 17:43:46 +01:00
|
|
|
var f = this._peer.connect(requestedPeer, {
|
|
|
|
label: 'file',
|
|
|
|
reliable: true
|
|
|
|
});
|
|
|
|
f.on('open', function() {
|
|
|
|
this.connect(f);
|
2015-12-23 13:57:13 +01:00
|
|
|
|
2015-12-18 17:43:46 +01:00
|
|
|
}.bind(this));
|
|
|
|
f.on('error', function(err) {
|
|
|
|
console.log(err);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}
|
2015-12-19 01:18:02 +01:00
|
|
|
return function(requestedPeer, callback) {
|
|
|
|
if (this._peer.connections[requestedPeer]) {
|
2015-12-19 02:12:45 +01:00
|
|
|
callback();
|
2015-12-19 01:18:02 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (this._peerOpen) {
|
|
|
|
request(requestedPeer, callback).bind(this)();
|
|
|
|
} else {
|
|
|
|
this._initCallbacks.push(request(requestedPeer, callback).bind(this));
|
2015-12-18 17:43:46 +01:00
|
|
|
}
|
2015-12-19 01:18:02 +01:00
|
|
|
|
2015-12-18 17:43:46 +01:00
|
|
|
};
|
|
|
|
}()),
|
2015-12-23 13:57:13 +01:00
|
|
|
_sendFile: function(peerId, file) {
|
2015-12-18 17:43:46 +01:00
|
|
|
var conns = this._peer.connections[peerId];
|
|
|
|
if (conns) {
|
2015-12-19 01:18:02 +01:00
|
|
|
conns.forEach(function(conn) {
|
2015-12-18 17:43:46 +01:00
|
|
|
if (conn.label === 'file') {
|
|
|
|
conn.send(file);
|
2015-12-30 18:07:37 +01:00
|
|
|
console.log('send file via WebRTC');
|
2015-12-18 17:43:46 +01:00
|
|
|
}
|
2015-12-23 13:57:13 +01:00
|
|
|
}.bind(this));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_sendSystemEvent: function(peerId, msg) {
|
|
|
|
var conns = this._peer.connections[peerId];
|
|
|
|
if (conns) {
|
|
|
|
conns.forEach(function(conn) {
|
|
|
|
if (conn.label === 'system') {
|
|
|
|
conn.send(msg);
|
|
|
|
}
|
|
|
|
}.bind(this));
|
2015-12-18 17:43:46 +01:00
|
|
|
}
|
2015-12-30 18:07:37 +01:00
|
|
|
},
|
|
|
|
_reconnect: function(e) {
|
|
|
|
//try to reconnect after 3s
|
|
|
|
if (!this.reconnectTimer) {
|
|
|
|
this.reconnectTimer = setInterval(this.initialize.bind(this), 3000);
|
|
|
|
}
|
2015-12-18 17:43:46 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
</script>
|
|
|
|
</dom-module>
|