file-transfer-protocol.html 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. <script>
  2. 'use strict';
  3. window.Chat = window.Chat || {};
  4. Chat.FileTransferProtocol = {
  5. properties: {
  6. loading: {
  7. type: Boolean,
  8. notify: true,
  9. value: false,
  10. observer: '_loadingChanged'
  11. },
  12. buddies: {
  13. notify: true
  14. }
  15. },
  16. listeners: {
  17. 'system-event': '_onSystemMsg',
  18. 'file-received': '_onFileReceived',
  19. },
  20. _onSystemMsg: function(event) {
  21. var msg = event.detail;
  22. console.log('FTP received sysMsg:', msg);
  23. switch (msg.type) {
  24. case 'offer':
  25. this._onOffered(msg);
  26. break;
  27. case 'decline':
  28. this._onDeclined(msg);
  29. break;
  30. case 'accept':
  31. this._onAccepted(msg);
  32. break;
  33. case 'transfer':
  34. this._onTransfer(msg);
  35. break;
  36. case 'received':
  37. this._onReceived(msg);
  38. break;
  39. case 'buddies':
  40. this._onBuddies(msg);
  41. break;
  42. }
  43. },
  44. sendFile: function(peerId, file) {
  45. this.set('loading', true);
  46. this.fileToSend = file;
  47. this.fire('file-offered', {
  48. to: peerId
  49. });
  50. this.connectToPeer(peerId, function() {
  51. this._offer(peerId, file);
  52. }.bind(this));
  53. //set 15sec timeout
  54. this._timeoutTimer = this.async(function() {
  55. this._onError();
  56. }, 15000);
  57. },
  58. _offer: function(toPeer, file) {
  59. console.log('FTP offer file:', file, 'To:', toPeer);
  60. this._sendSystemEvent(toPeer, {
  61. type: 'offer',
  62. name: file.name
  63. });
  64. },
  65. _onOffered: function(offer) {
  66. console.log('FTP offered file:', offer.name, 'From:', offer.from);
  67. this.fire('file-offer', {
  68. from: offer.from,
  69. name: offer.name
  70. });
  71. },
  72. decline: function(offer) {
  73. this._sendSystemEvent(offer.from, {
  74. type: 'decline',
  75. name: offer.name
  76. });
  77. },
  78. _onDeclined: function(offer) {
  79. this.cancelAsync(this._timeoutTimer);
  80. delete this.fileToSend;
  81. this.set('loading', false);
  82. this.fire('file-declined', offer);
  83. },
  84. accept: function(offer) {
  85. this._sendSystemEvent(offer.from, {
  86. type: 'accept',
  87. name: offer.name
  88. });
  89. this.fire('download-started', {
  90. from: offer.from
  91. });
  92. },
  93. _onAccepted: function(offer) {
  94. this.cancelAsync(this._timeoutTimer);
  95. this._sendSystemEvent(offer.from, {
  96. type: 'transfer',
  97. name: offer.name
  98. });
  99. this.fire('upload-started', {
  100. to: offer.from
  101. });
  102. this._sendFile(offer.from, this.fileToSend);
  103. },
  104. _onTransfer: function() {
  105. this.loading = true;
  106. },
  107. _onFileReceived: function(event) {
  108. var file = event.detail;
  109. this.loading = false;
  110. this._sendSystemEvent(file.from, {
  111. type: 'received',
  112. name: file.name
  113. });
  114. this.fire('download-complete', {
  115. from: file.from
  116. });
  117. console.log('FTP received:', file);
  118. },
  119. _onReceived: function(offer) {
  120. this.loading = false;
  121. this.fire('upload-complete', offer);
  122. },
  123. _onError: function() {
  124. this.loading = false;
  125. this.fire('upload-error');
  126. },
  127. _loadingChanged: function(loading) {
  128. window.anim(loading);
  129. },
  130. _onBuddies: function(msg) {
  131. this.set('buddies', msg.buddies);
  132. }
  133. };
  134. </script>