peer.js 4.5 KB

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