peer.js 4.6 KB

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