peer.js 7.1 KB

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