peer.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. function Peer(options) {
  2. if (!(this instanceof Peer)) return new Peer(options);
  3. EventEmitter.call(this);
  4. options = util.extend({
  5. debug: false,
  6. host: 'localhost',
  7. port: 80
  8. }, options);
  9. this.options = options;
  10. util.debug = options.debug;
  11. this._server = options.host + ':' + options.port;
  12. // Ensure alphanumeric_-
  13. if (options.id && !/^[A-Za-z0-9]+(?:[ _-][A-Za-z0-9]+)*$/.exec(options.id))
  14. throw new Error('Peer ID can only contain alphanumerics, "_", and "-".');
  15. this._id = options.id;
  16. // Not used unless using cloud server.
  17. this._apikey = options.apikey;
  18. // Check in with the server with ID or get an ID.
  19. // this._startXhrStream();
  20. this._checkIn();
  21. // Connections for this peer.
  22. this.connections = {};
  23. // Queued connections to make.
  24. this._queued = [];
  25. // Make sure connections are cleaned up.
  26. window.onbeforeunload = this._cleanup;
  27. };
  28. util.inherits(Peer, EventEmitter);
  29. /** Check in with ID or get one from server. */
  30. Peer.prototype._checkIn = function() {
  31. try {
  32. var http = new XMLHttpRequest();
  33. // If no ID provided, get a unique ID from server.
  34. var self = this;
  35. if (!this._id) {
  36. // If there's no ID we need to wait for one before trying to init socket.
  37. http.open('get', this._server + '/id', true);
  38. http.onreadystatechange = function() {
  39. if (http.readyState > 2) {
  40. if (!!http.responseText) {
  41. try {
  42. var response = JSON.parse(http.responseText);
  43. if (!!response.id) {
  44. self._id = response.id;
  45. }
  46. } catch (e) {
  47. // Ignore
  48. }
  49. }
  50. }
  51. self._socketInit();
  52. self._handleStream(http);
  53. };
  54. http.send(null);
  55. } else {
  56. this._socketInit();
  57. http.open('post', this._server + '/id', true);
  58. http.onreadystatechange = function() {
  59. self._handleStream(http);
  60. };
  61. http.send('id=' + this._id);
  62. }
  63. // TODO: may need to setInterval in case handleStream is not being called
  64. // enough.
  65. } catch(e) {
  66. util.log('XMLHttpRequest not available; defaulting to WebSockets');
  67. this._socketInit();
  68. }
  69. };
  70. /** Taking care of this in /id now.
  71. Peer.prototype._startXhrStream = function() {
  72. try {
  73. var http = new XMLHttpRequest();
  74. // If no ID provided, get a unique ID from server.
  75. var self = this;
  76. http.open('get', this._server + '/stream', true);
  77. http.onreadystatechange = function() {
  78. self._handleStream(http);
  79. };
  80. http.send(null);
  81. // TODO: may need to setInterval in case handleStream is not being called
  82. // enough.
  83. } catch(e) {
  84. util.log('XMLHttpRequest not available; defaulting to WebSockets');
  85. }
  86. };
  87. */
  88. /** Handles onreadystatechange response as a stream. */
  89. Peer.prototype._handleStream = function(http) {
  90. // 3 and 4 are loading/done state. All others are not relevant.
  91. if (http.readyState < 3) {
  92. return;
  93. } else if (http.readyState == 3 && http.status != 200) {
  94. return;
  95. } else if (http.readyState == 4 && http.status != 200) {
  96. // Clear setInterval here if using it.
  97. }
  98. if (http.responseText === null)
  99. return;
  100. // TODO: handle
  101. console.log(http.responseText);
  102. if (http.readyState == 4 && !this._socketOpen)
  103. this._startXhrStream();
  104. };
  105. /** Start up websocket communications. */
  106. Peer.prototype._socketInit = function() {
  107. this._socket = new WebSocket('ws://' + this._server + '/ws?id=' + this._id);
  108. var self = this;
  109. this._socket.onmessage = function(event) {
  110. var message = JSON.parse(event.data);
  111. var peer = message.src;
  112. var connection = self.connections[peer];
  113. switch (message.type) {
  114. case 'ID':
  115. if (!self._id) {
  116. // If we're just now getting an ID then we may have a queue.
  117. self._id = message.id;
  118. self._processQueue();
  119. }
  120. break;
  121. case 'OFFER':
  122. var options = {
  123. metadata: message.metadata,
  124. sdp: message.sdp
  125. };
  126. var connection = new DataConnection(self._id, peer, self._socket, function(err, connection) {
  127. if (!err) {
  128. self.emit('connection', connection, message.metadata);
  129. }
  130. }, options);
  131. self.connections[peer] = connection;
  132. break;
  133. case 'ANSWER':
  134. if (connection) connection.handleSDP(message);
  135. break;
  136. case 'CANDIDATE':
  137. if (connection) connection.handleCandidate(message);
  138. break;
  139. case 'LEAVE':
  140. if (connection) connection.handleLeave();
  141. break;
  142. case 'PORT':
  143. if (util.browserisms === 'Firefox') {
  144. connection.handlePort(message);
  145. break;
  146. }
  147. case 'DEFAULT':
  148. util.log('PEER: unrecognized message ', message.type);
  149. break;
  150. }
  151. };
  152. // Take care of the queue of connections if necessary and make sure Peer knows
  153. // socket is open.
  154. this._socket.onopen = function() {
  155. self._socketOpen = true;
  156. if (self._id)
  157. self._processQueue();
  158. };
  159. };
  160. Peer.prototype._processQueue = function() {
  161. while (this._queued.length > 0) {
  162. var cdata = this._queued.pop();
  163. this.connect.apply(this, cdata);
  164. }
  165. };
  166. Peer.prototype._cleanup = function() {
  167. for (var peer in this.connections) {
  168. if (this.connections.hasOwnProperty(peer)) {
  169. this.connections[peer].close();
  170. }
  171. }
  172. };
  173. Peer.prototype.connect = function(peer, metadata, cb) {
  174. if (typeof metadata === 'function' && !cb) cb = metadata; metadata = false;
  175. if (!this._id) {
  176. this._queued.push(Array.prototype.slice.apply(arguments));
  177. return;
  178. }
  179. var options = {
  180. metadata: metadata
  181. };
  182. var connection = new DataConnection(this._id, peer, this._socket, cb, options);
  183. this.connections[peer] = connection;
  184. };
  185. exports.Peer = Peer;