peer.js 6.0 KB

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