57 lines
		
	
	
		
			2.0 KiB
		
	
	
	
		
			HTML
		
	
	
	
			
		
		
	
	
			57 lines
		
	
	
		
			2.0 KiB
		
	
	
	
		
			HTML
		
	
	
	
<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() {
 | 
						|
        window.webRTCSupported = !!(window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection || window.webkitRTCPeerConnection);
 | 
						|
 | 
						|
        function rtcConnectionSupported(peerId) {
 | 
						|
            return window.webRTCSupported && (peerId.indexOf('rtc_') === 0);
 | 
						|
        }
 | 
						|
        Polymer({
 | 
						|
            is: 'connection-wrapper',
 | 
						|
            properties: {
 | 
						|
                me: {
 | 
						|
                    notify: true
 | 
						|
                },
 | 
						|
            },
 | 
						|
            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 {
 | 
						|
                    this.$.p2p.connectToPeer(toPeer, callback);
 | 
						|
                }
 | 
						|
            },
 | 
						|
            _onHandshake: function(event) {
 | 
						|
                var me = event.uuid;
 | 
						|
                this.set('me', me);
 | 
						|
                if (window.webRTCSupported) {
 | 
						|
                    this.$.p2p.initialize();
 | 
						|
                }
 | 
						|
            }
 | 
						|
        });
 | 
						|
    })();
 | 
						|
    </script>
 | 
						|
</dom-module>
 |