2015-12-23 13:57:13 +01:00
|
|
|
<link rel="import" href="binaryjs.html">
|
|
|
|
<dom-module id="web-socket">
|
|
|
|
<template>
|
|
|
|
<style>
|
|
|
|
:host {
|
|
|
|
display: block;
|
|
|
|
}
|
|
|
|
</style>
|
|
|
|
</template>
|
|
|
|
<script>
|
|
|
|
'use strict';
|
|
|
|
Polymer({
|
|
|
|
is: 'web-socket',
|
|
|
|
attached: function() {
|
|
|
|
this.init();
|
|
|
|
},
|
|
|
|
init: function() {
|
2015-12-31 00:42:53 +01:00
|
|
|
clearInterval(this.reconnectTimer);
|
|
|
|
this.reconnectTimer = undefined;
|
|
|
|
var websocketUrl = (window.debug ? 'ws://' + window.location.hostname + ':3002' : 'wss://snapdrop.net') + '/binary';
|
|
|
|
this.client = new BinaryClient(websocketUrl);
|
|
|
|
this.client.on('stream', function(stream, meta) {
|
|
|
|
// collect stream data
|
|
|
|
var parts = [];
|
|
|
|
stream.on('data', function(data) {
|
|
|
|
//console.log('part received', meta, data);
|
|
|
|
if (data.isSystemEvent) {
|
|
|
|
if (meta) {
|
|
|
|
data.from = meta.from;
|
2015-12-23 13:57:13 +01:00
|
|
|
}
|
2015-12-31 00:42:53 +01:00
|
|
|
this.fire('system-event', data);
|
|
|
|
} else {
|
|
|
|
parts.push(data);
|
|
|
|
}
|
2015-12-23 13:57:13 +01:00
|
|
|
}.bind(this));
|
2015-12-31 00:42:53 +01:00
|
|
|
// when finished, set it as the background image
|
|
|
|
stream.on('end', function() {
|
|
|
|
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
|
2015-12-23 13:57:13 +01:00
|
|
|
});
|
|
|
|
}.bind(this));
|
2015-12-31 00:42:53 +01:00
|
|
|
}.bind(this));
|
|
|
|
this.client.on('open', function(e) {
|
|
|
|
console.log(e);
|
|
|
|
this.client.send({}, {
|
|
|
|
serverMsg: 'rtc-support',
|
|
|
|
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));
|
2015-12-23 13:57:13 +01:00
|
|
|
},
|
|
|
|
_sendFile: function(toPeer, file) {
|
2015-12-26 13:33:16 +01:00
|
|
|
console.log('send file via WebSocket', file);
|
2015-12-23 13:57:13 +01:00
|
|
|
this.client.send(file.file, {
|
|
|
|
name: file.file.name,
|
|
|
|
type: file.file.type,
|
|
|
|
toPeer: toPeer
|
|
|
|
});
|
|
|
|
},
|
|
|
|
connectToPeer: function(peer, callback) {
|
|
|
|
callback();
|
|
|
|
},
|
|
|
|
_sendSystemEvent: function(toPeer, event) {
|
|
|
|
console.log('system event', toPeer, event);
|
|
|
|
event.isSystemEvent = true;
|
|
|
|
this.client.send(event, {
|
|
|
|
toPeer: toPeer
|
|
|
|
});
|
2015-12-26 13:33:16 +01:00
|
|
|
},
|
|
|
|
_reconnect: function(e) {
|
|
|
|
console.log('disconnected', e);
|
|
|
|
//try to reconnect after 3s
|
2015-12-30 18:07:37 +01:00
|
|
|
if (!this.reconnectTimer) {
|
|
|
|
this.reconnectTimer = setInterval(this.init.bind(this), 3000);
|
|
|
|
}
|
2015-12-23 13:57:13 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
</script>
|
|
|
|
</dom-module>
|