2015-12-23 13:57:13 +01:00
|
|
|
<link rel="import" href="p2p-network.html">
|
|
|
|
<link rel="import" href="web-socket.html">
|
|
|
|
<dom-module id="connection-wrapper">
|
|
|
|
<template>
|
|
|
|
<p2p-network id="p2p" me="{{me}}"></p2p-network>
|
|
|
|
<web-socket id="ws" me="{{me}}"></web-socket>
|
|
|
|
</template>
|
|
|
|
<script>
|
|
|
|
'use strict';
|
|
|
|
(function() {
|
2015-12-26 13:33:16 +01:00
|
|
|
window.webRTCSupported = !!(window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection || window.webkitRTCPeerConnection);
|
2015-12-23 13:57:13 +01:00
|
|
|
|
|
|
|
function rtcConnectionSupported(peerId) {
|
2015-12-26 13:33:16 +01:00
|
|
|
return window.webRTCSupported && (peerId.indexOf('rtc_') === 0);
|
2015-12-23 13:57:13 +01:00
|
|
|
}
|
|
|
|
Polymer({
|
|
|
|
is: 'connection-wrapper',
|
|
|
|
properties: {
|
|
|
|
me: {
|
2015-12-26 13:33:16 +01:00
|
|
|
notify: true
|
|
|
|
},
|
2015-12-23 13:57:13 +01:00
|
|
|
},
|
|
|
|
behaviors: [Chat.FileTransferProtocol],
|
|
|
|
_sendFile: function(toPeer, file) {
|
|
|
|
if (!rtcConnectionSupported(toPeer)) {
|
|
|
|
this.$.ws._sendFile(toPeer, file);
|
|
|
|
} else {
|
|
|
|
this.$.p2p._sendFile(toPeer, file);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
_sendSystemEvent: function(toPeer, event) {
|
|
|
|
console.log('system event', toPeer, event);
|
|
|
|
if (!rtcConnectionSupported(toPeer)) {
|
|
|
|
this.$.ws._sendSystemEvent(toPeer, event);
|
|
|
|
} else {
|
|
|
|
this.$.p2p._sendSystemEvent(toPeer, event);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
connectToPeer: function(toPeer, callback) {
|
|
|
|
if (!rtcConnectionSupported(toPeer)) {
|
|
|
|
callback();
|
|
|
|
} else {
|
2015-12-26 13:33:16 +01:00
|
|
|
this.$.p2p.connectToPeer(toPeer, callback);
|
2015-12-23 13:57:13 +01:00
|
|
|
}
|
|
|
|
},
|
2015-12-26 13:33:16 +01:00
|
|
|
_onHandshake: function(event) {
|
|
|
|
var me = event.uuid;
|
|
|
|
this.set('me', me);
|
|
|
|
if (window.webRTCSupported) {
|
|
|
|
this.$.p2p.initialize();
|
|
|
|
}
|
|
|
|
}
|
2015-12-23 13:57:13 +01:00
|
|
|
});
|
|
|
|
})();
|
|
|
|
</script>
|
|
|
|
</dom-module>
|