p2p-network.html 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <script src="../../../bower_components/peerjs/peer.min.js"></script>
  2. <dom-module id="p2p-network">
  3. <template>
  4. </template>
  5. <script>
  6. 'use strict';
  7. Polymer({
  8. is: 'p2p-network',
  9. properties: {
  10. me: {
  11. type: String,
  12. notify: true,
  13. }
  14. },
  15. attached: function() {
  16. this._connectedPeers = {};
  17. this._initCallbacks = [];
  18. this._unsendMsgs = {};
  19. window.onunload = window.onbeforeunload = function() {
  20. if (!!this._peer && !this._peer.destroyed) {
  21. this._peer.destroy();
  22. }
  23. }.bind(this);
  24. this._initialize();
  25. },
  26. _initialize: function() {
  27. var options = {
  28. host: 'yawim.com',
  29. port: 443,
  30. path: 'peerjs',
  31. secure: true
  32. };
  33. this._peer = new Peer(options);
  34. this._peer.on('open', function(id) {
  35. console.log('My peer ID is: ' + id);
  36. this.set('me', id);
  37. this._peerOpen = true;
  38. this._initCallbacks.forEach(function(cb) {
  39. cb();
  40. });
  41. }.bind(this));
  42. this._peer.on('connection', this.connect.bind(this));
  43. this._peer.on('error', function(err) {
  44. console.error(err);
  45. //ugly hack to find out error type
  46. if (err.message.indexOf('Could not connect to peer') > -1) {
  47. delete this._connectedPeers[this.peer];
  48. this.set('peer', 'error');
  49. return;
  50. }
  51. if (err.message.indexOf('Lost connection to server') > -1) {
  52. this._peer.destroy();
  53. this.set('me', this.me);
  54. this._initialize();
  55. return;
  56. }
  57. }.bind(this));
  58. },
  59. connect: function(c) {
  60. var peer = c.peer;
  61. if (c.label === 'file') {
  62. c.on('data', function(data) {
  63. console.log('received!', data);
  64. this.fire('file-received', {
  65. peer: peer,
  66. dataURI: data.dataURI,
  67. name: data.name,
  68. });
  69. }.bind(this));
  70. }
  71. },
  72. connectToPeer: (function() {
  73. function request(requestedPeer, callback) {
  74. return function() {
  75. var f = this._peer.connect(requestedPeer, {
  76. label: 'file',
  77. reliable: true
  78. });
  79. f.on('open', function() {
  80. this.connect(f);
  81. if (callback) {
  82. callback();
  83. }
  84. }.bind(this));
  85. f.on('error', function(err) {
  86. console.log(err);
  87. });
  88. };
  89. }
  90. return function(requestedPeer, callback) {
  91. if (this._peer.connections[requestedPeer]) {
  92. callback();
  93. return;
  94. }
  95. this.set('loading', true);
  96. if (this._peerOpen) {
  97. request(requestedPeer, callback).bind(this)();
  98. } else {
  99. this._initCallbacks.push(request(requestedPeer, callback).bind(this));
  100. }
  101. };
  102. }()),
  103. sendFile: function(peerId, file) {
  104. var conns = this._peer.connections[peerId];
  105. if (conns) {
  106. conns.forEach(function(conn) {
  107. if (conn.label === 'file') {
  108. conn.send(file);
  109. console.log('file send');
  110. }
  111. });
  112. }
  113. }
  114. });
  115. </script>
  116. </dom-module>