Keep connections alive

This commit is contained in:
Robin Linus 2015-12-31 00:42:53 +01:00
parent 3b4784e8eb
commit 00aa38564c
2 changed files with 80 additions and 94 deletions

View file

@ -23,49 +23,47 @@
}.bind(this); }.bind(this);
}, },
initialize: function() { initialize: function() {
if (window.isActive) { clearInterval(this.reconnectTimer);
clearInterval(this.reconnectTimer); this.reconnectTimer = undefined;
this.reconnectTimer = undefined; var options;
var options; if (window.debug) {
if (window.debug) { options = {
options = { host: window.location.hostname,
host: window.location.hostname, port: 3002,
port: 3002, path: 'peerjs'
path: 'peerjs' };
}; } else {
} else { options = {
options = { host: 'snapdrop.net',
host: 'snapdrop.net', port: 443,
port: 443, path: 'peerjs',
path: 'peerjs', secure: true
secure: true };
};
}
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));
} }
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));
}, },
connect: function(c) { connect: function(c) {

View file

@ -9,68 +9,56 @@
</template> </template>
<script> <script>
'use strict'; 'use strict';
window.isActive = true;
window.onfocus = function() {
window.isActive = true;
};
window.onblur = function() {
window.isActive = false;
};
Polymer({ Polymer({
is: 'web-socket', is: 'web-socket',
attached: function() { attached: function() {
this.init(); this.init();
}, },
init: function() { init: function() {
if (window.isActive) { clearInterval(this.reconnectTimer);
clearInterval(this.reconnectTimer); this.reconnectTimer = undefined;
this.reconnectTimer = undefined; var websocketUrl = (window.debug ? 'ws://' + window.location.hostname + ':3002' : 'wss://snapdrop.net') + '/binary';
var websocketUrl = (window.debug ? 'ws://' + window.location.hostname + ':3002' : 'wss://snapdrop.net') + '/binary'; this.client = new BinaryClient(websocketUrl);
this.client = new BinaryClient(websocketUrl); this.client.on('stream', function(stream, meta) {
this.client.on('stream', function(stream, meta) { // collect stream data
// collect stream data var parts = [];
var parts = []; stream.on('data', function(data) {
stream.on('data', function(data) { //console.log('part received', meta, data);
//console.log('part received', meta, data); if (data.isSystemEvent) {
if (data.isSystemEvent) { if (meta) {
if (meta) { data.from = meta.from;
data.from = meta.from;
}
this.fire('system-event', data);
} else {
parts.push(data);
} }
}.bind(this)); this.fire('system-event', data);
// when finished, set it as the background image } else {
stream.on('end', function() { parts.push(data);
var blob = new Blob(parts, { }
type: meta.type
});
console.log('file received', blob, meta);
this.fire('file-received', {
blob: blob,
name: meta.name,
from: meta.from
});
}.bind(this));
}.bind(this)); }.bind(this));
this.client.on('open', function(e) { // when finished, set it as the background image
console.log(e); stream.on('end', function() {
this.client.send({}, { var blob = new Blob(parts, {
serverMsg: 'rtc-support', type: meta.type
rtc: window.webRTCSupported });
console.log('file received', blob, meta);
this.fire('file-received', {
blob: blob,
name: meta.name,
from: meta.from
}); });
}.bind(this)); }.bind(this));
this.client.on('error', function(e) { }.bind(this));
this._reconnect(e); this.client.on('open', function(e) {
}.bind(this)); console.log(e);
this.client.on('close', function(e) { this.client.send({}, {
this._reconnect(e); serverMsg: 'rtc-support',
}.bind(this)); rtc: window.webRTCSupported
} });
}.bind(this));
this.client.on('error', function(e) {
this._reconnect(e);
}.bind(this));
this.client.on('close', function(e) {
this._reconnect(e);
}.bind(this));
}, },
_sendFile: function(toPeer, file) { _sendFile: function(toPeer, file) {
console.log('send file via WebSocket', file); console.log('send file via WebSocket', file);