141 lines
		
	
	
		
			3.7 KiB
		
	
	
	
		
			HTML
		
	
	
	
			
		
		
	
	
			141 lines
		
	
	
		
			3.7 KiB
		
	
	
	
		
			HTML
		
	
	
	
<script>
 | 
						|
'use strict';
 | 
						|
window.Chat = window.Chat || {};
 | 
						|
Chat.FileTransferProtocol = {
 | 
						|
    properties: {
 | 
						|
        loading: {
 | 
						|
            type: Boolean,
 | 
						|
            notify: true,
 | 
						|
            value: false,
 | 
						|
            observer: '_loadingChanged'
 | 
						|
        },
 | 
						|
        buddies: {
 | 
						|
            notify: true
 | 
						|
        }
 | 
						|
    },
 | 
						|
    listeners: {
 | 
						|
        'system-event': '_onSystemMsg',
 | 
						|
        'file-received': '_onFileReceived',
 | 
						|
    },
 | 
						|
    _onSystemMsg: function(event) {
 | 
						|
        var msg = event.detail;
 | 
						|
        console.log('FTP received sysMsg:', msg);
 | 
						|
 | 
						|
        switch (msg.type) {
 | 
						|
            case 'handshake':
 | 
						|
                this._onHandshake(msg);
 | 
						|
                break;
 | 
						|
            case 'offer':
 | 
						|
                this._onOffered(msg);
 | 
						|
                break;
 | 
						|
            case 'decline':
 | 
						|
                this._onDeclined(msg);
 | 
						|
                break;
 | 
						|
            case 'accept':
 | 
						|
                this._onAccepted(msg);
 | 
						|
                break;
 | 
						|
            case 'transfer':
 | 
						|
                this._onTransfer(msg);
 | 
						|
                break;
 | 
						|
            case 'received':
 | 
						|
                this._onReceived(msg);
 | 
						|
                break;
 | 
						|
            case 'buddies':
 | 
						|
                this._onBuddies(msg);
 | 
						|
                break;
 | 
						|
        }
 | 
						|
    },
 | 
						|
    sendFile: function(peerId, file) {
 | 
						|
        this.set('loading', true);
 | 
						|
        this.fileToSend = file;
 | 
						|
        this.fire('file-offered', {
 | 
						|
            to: peerId
 | 
						|
        });
 | 
						|
        this.connectToPeer(peerId, function() {
 | 
						|
            this._offer(peerId, file);
 | 
						|
        }.bind(this));
 | 
						|
 | 
						|
        //set 15sec timeout
 | 
						|
        this._timeoutTimer = this.async(function() {
 | 
						|
            this._onError();
 | 
						|
        }, 15000);
 | 
						|
    },
 | 
						|
    _offer: function(toPeer, file) {
 | 
						|
        console.log('FTP offer file:', file, 'To:', toPeer);
 | 
						|
 | 
						|
        this._sendSystemEvent(toPeer, {
 | 
						|
            type: 'offer',
 | 
						|
            name: file.name
 | 
						|
        });
 | 
						|
    },
 | 
						|
    _onOffered: function(offer) {
 | 
						|
        console.log('FTP offered file:', offer.name, 'From:', offer.from);
 | 
						|
        this.fire('file-offer', {
 | 
						|
            from: offer.from,
 | 
						|
            name: offer.name
 | 
						|
        });
 | 
						|
    },
 | 
						|
    decline: function(offer) {
 | 
						|
        this._sendSystemEvent(offer.from, {
 | 
						|
            type: 'decline',
 | 
						|
            name: offer.name
 | 
						|
        });
 | 
						|
    },
 | 
						|
    _onDeclined: function(offer) {
 | 
						|
        this.cancelAsync(this._timeoutTimer);
 | 
						|
        delete this.fileToSend;
 | 
						|
        this.set('loading', false);
 | 
						|
        this.fire('file-declined', offer);
 | 
						|
    },
 | 
						|
    accept: function(offer) {
 | 
						|
        this._sendSystemEvent(offer.from, {
 | 
						|
            type: 'accept',
 | 
						|
            name: offer.name
 | 
						|
        });
 | 
						|
        this.fire('download-started', {
 | 
						|
            from: offer.from
 | 
						|
        });
 | 
						|
    },
 | 
						|
    _onAccepted: function(offer) {
 | 
						|
        this.cancelAsync(this._timeoutTimer);
 | 
						|
        this._sendSystemEvent(offer.from, {
 | 
						|
            type: 'transfer',
 | 
						|
            name: offer.name
 | 
						|
        });
 | 
						|
        this.fire('upload-started', {
 | 
						|
            to: offer.from
 | 
						|
        });
 | 
						|
        this._sendFile(offer.from, this.fileToSend);
 | 
						|
    },
 | 
						|
    _onTransfer: function() {
 | 
						|
        this.loading = true;
 | 
						|
    },
 | 
						|
    _onFileReceived: function(event) {
 | 
						|
        var file = event.detail;
 | 
						|
        this.loading = false;
 | 
						|
        this._sendSystemEvent(file.from, {
 | 
						|
            type: 'received',
 | 
						|
            name: file.name
 | 
						|
        });
 | 
						|
        this.fire('download-complete', {
 | 
						|
            from: file.from
 | 
						|
        });
 | 
						|
        console.log('FTP received:', file);
 | 
						|
    },
 | 
						|
    _onReceived: function(offer) {
 | 
						|
        this.loading = false;
 | 
						|
        this.fire('upload-complete', offer);
 | 
						|
    },
 | 
						|
    _onError: function() {
 | 
						|
        this.loading = false;
 | 
						|
        this.fire('upload-error');
 | 
						|
    },
 | 
						|
    _loadingChanged: function(loading) {
 | 
						|
        window.anim(loading);
 | 
						|
    },
 | 
						|
    _onBuddies: function(msg) {
 | 
						|
        this.set('buddies', msg.buddies);
 | 
						|
    }
 | 
						|
};
 | 
						|
</script>
 |