peer.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 'ID-TAKEN':
  70. this.emit('error', message.msg);
  71. this.destroy('ID `'+this._id+'` is taken');
  72. break;
  73. case 'OFFER':
  74. var options = {
  75. metadata: message.metadata,
  76. sdp: message.sdp,
  77. config: this._options.config,
  78. };
  79. var self = this;
  80. var connection = new DataConnection(this._id, peer, this._socket, function(err, connection) {
  81. if (!err) {
  82. self.emit('connection', connection, message.metadata);
  83. }
  84. }, options);
  85. this._attachConnectionListeners(connection);
  86. this.connections[peer] = connection;
  87. break;
  88. case 'EXPIRE':
  89. if (connection) {
  90. connection.close('Could not connect to peer ' + connection._peer);
  91. }
  92. break;
  93. case 'ANSWER':
  94. if (connection) {
  95. connection.handleSDP(message);
  96. }
  97. break;
  98. case 'CANDIDATE':
  99. if (connection) {
  100. connection.handleCandidate(message);
  101. }
  102. break;
  103. case 'LEAVE':
  104. if (connection) {
  105. connection.handleLeave();
  106. }
  107. break;
  108. case 'PORT':
  109. //if (util.browserisms === 'Firefox') {
  110. // connection.handlePort(message);
  111. // break;
  112. //}
  113. case 'DEFAULT':
  114. util.log('Unrecognized message type:', message.type);
  115. break;
  116. }
  117. };
  118. /** Process queued calls to connect. */
  119. Peer.prototype._processQueue = function() {
  120. while (this._queued.length > 0) {
  121. var cdata = this._queued.pop();
  122. this.connect.apply(this, cdata);
  123. }
  124. };
  125. Peer.prototype._cleanup = function(reason) {
  126. var self = this;
  127. var peers = Object.keys(this.connections);
  128. for (var i = 0, ii = peers.length; i < ii; i++) {
  129. this.connections[peers[i]].close(reason);
  130. }
  131. util.setZeroTimeout(function(){
  132. self._socket.close();
  133. });
  134. };
  135. /** Listeners for DataConnection events. */
  136. Peer.prototype._attachConnectionListeners = function(connection) {
  137. var self = this;
  138. connection.on('close', function(peer) {
  139. if (self.connections[peer]) {
  140. delete self.connections[peer];
  141. }
  142. });
  143. };
  144. /** Exposed connect function for users. Will try to connect later if user
  145. * is waiting for an ID. */
  146. // TODO: pause XHR streaming when not in use and start again when this is
  147. // called.
  148. Peer.prototype.connect = function(peer, metadata, cb) {
  149. if (typeof metadata === 'function' && !cb) cb = metadata; metadata = false;
  150. if (!this._id) {
  151. this._queued.push(Array.prototype.slice.apply(arguments));
  152. return;
  153. }
  154. var options = {
  155. metadata: metadata,
  156. config: this._options.config,
  157. };
  158. var connection = new DataConnection(this._id, peer, this._socket, cb, options);
  159. this._attachConnectionListeners(connection);
  160. this.connections[peer] = connection;
  161. };
  162. Peer.prototype.destroy = function(reason) {
  163. this._cleanup(reason);
  164. };
  165. exports.Peer = Peer;