file-transfer-protocol.html 3.7 KB

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