p2p-network.html 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <script src="../../bower_components/peerjs/peer.min.js"></script>
  2. <link rel="import" href="file-transfer-protocol.html">
  3. <dom-module id="p2p-network">
  4. <template>
  5. </template>
  6. <script>
  7. 'use strict';
  8. Polymer({
  9. is: 'p2p-network',
  10. properties: {
  11. me: {
  12. type: String,
  13. notify: true,
  14. }
  15. },
  16. attached: function() {
  17. this._connectedPeers = {};
  18. this._initCallbacks = [];
  19. this._unsendMsgs = {};
  20. window.onunload = window.onbeforeunload = function() {
  21. if (!!this._peer && !this._peer.destroyed) {
  22. this._peer.destroy();
  23. }
  24. }.bind(this);
  25. this._initialize();
  26. },
  27. _initialize: function() {
  28. var options = {
  29. host: 'yawim.com',
  30. port: 443,
  31. path: 'peerjs',
  32. secure: true
  33. };
  34. this._peer = new Peer(this.me,options);
  35. this._peer.on('open', function(id) {
  36. console.log('My peer ID is: ' + id);
  37. this.set('me', id);
  38. this._peerOpen = true;
  39. this._initCallbacks.forEach(function(cb) {
  40. cb();
  41. });
  42. }.bind(this));
  43. this._peer.on('connection', this.connect.bind(this));
  44. this._peer.on('error', function(err) {
  45. console.error(err);
  46. //ugly hack to find out error type
  47. if (err.message.indexOf('Could not connect to peer') > -1) {
  48. delete this._connectedPeers[this.peer];
  49. this.set('peer', 'error');
  50. return;
  51. }
  52. if (err.message.indexOf('Lost connection to server') > -1) {
  53. this._peer.destroy();
  54. this.set('me', this.me);
  55. this._initialize();
  56. return;
  57. }
  58. }.bind(this));
  59. },
  60. connect: function(c) {
  61. var peer = c.peer;
  62. if (c.label === 'file') {
  63. c.on('data', function(data) {
  64. console.log(data);
  65. var dataView = new Uint8Array(data.file);
  66. var dataBlob = new Blob([dataView]);
  67. this.fire('file-received', {
  68. from: peer,
  69. blob: dataBlob,
  70. name: data.name,
  71. });
  72. }.bind(this));
  73. }
  74. if (c.label === 'system') {
  75. c.on('data', function(data) {
  76. data.from = peer;
  77. this.fire('system-event', data);
  78. }.bind(this));
  79. }
  80. },
  81. connectToPeer: (function() {
  82. function request(requestedPeer, callback) {
  83. return function() {
  84. //system messages channel
  85. var s = this._peer.connect(requestedPeer, {
  86. label: 'system'
  87. });
  88. s.on('open', function() {
  89. this.connect(s);
  90. if (callback) {
  91. callback();
  92. }
  93. }.bind(this));
  94. s.on('error', function(err) {
  95. console.log(err);
  96. if (err.message.indexOf('Connection is not open') > -1) {
  97. console.err('Handle this error!!');
  98. }
  99. });
  100. //files channel
  101. var f = this._peer.connect(requestedPeer, {
  102. label: 'file',
  103. reliable: true
  104. });
  105. f.on('open', function() {
  106. this.connect(f);
  107. }.bind(this));
  108. f.on('error', function(err) {
  109. console.log(err);
  110. });
  111. };
  112. }
  113. return function(requestedPeer, callback) {
  114. if (this._peer.connections[requestedPeer]) {
  115. callback();
  116. return;
  117. }
  118. if (this._peerOpen) {
  119. request(requestedPeer, callback).bind(this)();
  120. } else {
  121. this._initCallbacks.push(request(requestedPeer, callback).bind(this));
  122. }
  123. };
  124. }()),
  125. _sendFile: function(peerId, file) {
  126. var conns = this._peer.connections[peerId];
  127. if (conns) {
  128. conns.forEach(function(conn) {
  129. if (conn.label === 'file') {
  130. conn.send(file);
  131. console.log('file send');
  132. }
  133. }.bind(this));
  134. }
  135. },
  136. _sendSystemEvent: function(peerId, msg) {
  137. var conns = this._peer.connections[peerId];
  138. if (conns) {
  139. conns.forEach(function(conn) {
  140. if (conn.label === 'system') {
  141. conn.send(msg);
  142. }
  143. }.bind(this));
  144. }
  145. }
  146. });
  147. </script>
  148. </dom-module>