peer.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /**
  2. * A peer who can initiate connections with other peers.
  3. */
  4. function Peer(options) {
  5. if (!(this instanceof Peer)) return new Peer(options);
  6. EventEmitter.call(this);
  7. options = util.extend({
  8. debug: false,
  9. host: '0.peerjs.com',
  10. config: { 'iceServers': [{ 'url': 'stun:stun.l.google.com:19302' }] },
  11. port: 80
  12. }, options);
  13. this._options = options;
  14. util.debug = options.debug;
  15. this._server = options.host + ':' + options.port;
  16. // Ensure alphanumeric_-
  17. if (options.id && !/^[A-Za-z0-9]+(?:[ _-][A-Za-z0-9]+)*$/.exec(options.id)) {
  18. throw new Error('Peer ID can only contain alphanumerics, "_", and "-".');
  19. }
  20. if (options.key && !/^[A-Za-z0-9]+(?:[ _-][A-Za-z0-9]+)*$/.exec(options.key)) {
  21. throw new Error('API key can only contain alphanumerics, "_", and "-".');
  22. }
  23. this._id = options.id;
  24. // Not used unless using cloud server.
  25. this._key = options.key;
  26. this._startSocket();
  27. // Connections for this peer.
  28. this.connections = {};
  29. // Queued connections to make.
  30. this._queued = [];
  31. };
  32. util.inherits(Peer, EventEmitter);
  33. Peer.prototype._startSocket = function() {
  34. var self = this;
  35. this._socket = new Socket(this._server, this._id, this._key);
  36. this._socket.on('message', function(data) {
  37. self._handleServerJSONMessage(data);
  38. });
  39. this._socket.on('open', function() {
  40. self._processQueue();
  41. });
  42. this._socket.on('unavailable', function(peer) {
  43. util.log('Destination peer not available.', peer);
  44. if (self.connections[peer]) {
  45. self.connections[peer].close();
  46. }
  47. });
  48. this._socket.on('error', function(error) {
  49. util.log(error);
  50. });
  51. this._socket.start();
  52. }
  53. Peer.prototype._handleServerJSONMessage = function(message) {
  54. var peer = message.src;
  55. var connection = this.connections[peer];
  56. switch (message.type) {
  57. case 'ID':
  58. if (!this._id) {
  59. // If we're just now getting an ID then we may have a queue.
  60. this._id = message.id;
  61. this.emit('ready', this._id);
  62. this._processQueue();
  63. }
  64. break;
  65. case 'ERROR':
  66. this.emit('error', message.msg);
  67. util.log(message.msg);
  68. break;
  69. case 'OFFER':
  70. var options = {
  71. metadata: message.metadata,
  72. sdp: message.sdp,
  73. config: this._options.config,
  74. };
  75. var self = this;
  76. var connection = new DataConnection(this._id, peer, this._socket, function(err, connection) {
  77. if (!err) {
  78. self.emit('connection', connection, message.metadata);
  79. }
  80. }, options);
  81. this._attachConnectionListeners(connection);
  82. this.connections[peer] = connection;
  83. break;
  84. case 'EXPIRE':
  85. if (connection) {
  86. connection.close();
  87. }
  88. break;
  89. case 'ANSWER':
  90. if (connection) {
  91. connection.handleSDP(message);
  92. }
  93. break;
  94. case 'CANDIDATE':
  95. if (connection) {
  96. connection.handleCandidate(message);
  97. }
  98. break;
  99. case 'LEAVE':
  100. if (connection) {
  101. connection.handleLeave();
  102. }
  103. break;
  104. case 'PORT':
  105. //if (util.browserisms === 'Firefox') {
  106. // connection.handlePort(message);
  107. // break;
  108. //}
  109. case 'DEFAULT':
  110. util.log('Unrecognized message type:', message.type);
  111. break;
  112. }
  113. };
  114. /** Process queued calls to connect. */
  115. Peer.prototype._processQueue = function() {
  116. while (this._queued.length > 0) {
  117. var cdata = this._queued.pop();
  118. this.connect.apply(this, cdata);
  119. }
  120. };
  121. Peer.prototype._cleanup = function() {
  122. var self = this;
  123. var peers = Object.keys(this.connections);
  124. for (var i = 0, ii = peers.length; i < ii; i++) {
  125. this.connections[peers[i]].close();
  126. }
  127. util.setZeroTimeout(function(){
  128. self._socket.close();
  129. });
  130. };
  131. /** Listeners for DataConnection events. */
  132. Peer.prototype._attachConnectionListeners = function(connection) {
  133. var self = this;
  134. connection.on('close', function(peer) {
  135. if (self.connections[peer]) {
  136. delete self.connections[peer];
  137. }
  138. });
  139. };
  140. /** Exposed connect function for users. Will try to connect later if user
  141. * is waiting for an ID. */
  142. // TODO: pause XHR streaming when not in use and start again when this is
  143. // called.
  144. Peer.prototype.connect = function(peer, metadata, cb) {
  145. if (typeof metadata === 'function' && !cb) cb = metadata; metadata = false;
  146. if (!this._id) {
  147. this._queued.push(Array.prototype.slice.apply(arguments));
  148. return;
  149. }
  150. var options = {
  151. metadata: metadata,
  152. config: this._options.config,
  153. };
  154. var connection = new DataConnection(this._id, peer, this._socket, cb, options);
  155. this._attachConnectionListeners(connection);
  156. this.connections[peer] = connection;
  157. };
  158. Peer.prototype.destroy = function() {
  159. this._cleanup();
  160. };
  161. exports.Peer = Peer;