123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156 |
- <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;
- case 'text':
- this._onTextReceived(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);
- },
- sendText: function(toPeer, text) {
- console.log('FTP send text:', text, 'To:', toPeer);
- this.connectToPeer(toPeer, function() {
- this._sendSystemEvent(toPeer, {
- type: 'text',
- text: text
- });
- }.bind(this));
- },
- _onTextReceived: function(msg) {
- this.fire('text-received', msg);
- }
- };
- </script>
|