file-transfer-protocol.html 4.2 KB

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