peer.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. var ids = Object.keys(self.connections)
  123. for (var i = 0, ii = ids.length; i < ii; i += 1) {
  124. self.connections[ids[i]].setSocketOpen();
  125. }
  126. if (self._id)
  127. self._processQueue();
  128. };
  129. };
  130. Peer.prototype._handleServerMessage = function(message) {
  131. var message;
  132. try {
  133. message = JSON.parse(message);
  134. } catch(e) {
  135. util.log('message unrecognizable:', message);
  136. return;
  137. }
  138. var peer = message.src;
  139. var connection = this.connections[peer];
  140. switch (message.type) {
  141. case 'ID':
  142. if (!this._id) {
  143. // If we're just now getting an ID then we may have a queue.
  144. this._id = message.id;
  145. this.emit('ready', this._id);
  146. this._processQueue();
  147. }
  148. break;
  149. case 'ERROR':
  150. //throw new Error(message.msg);
  151. this.emit('error', message.msg);
  152. util.log(message.msg);
  153. break;
  154. case 'OFFER':
  155. var options = {
  156. metadata: message.metadata,
  157. sdp: message.sdp,
  158. socketOpen: this._socketOpen,
  159. config: this._config
  160. };
  161. var self = this;
  162. var connection = new DataConnection(this._id, peer, this._socket, this._httpUrl, function(err, connection) {
  163. if (!err) {
  164. self.emit('connection', connection, message.metadata);
  165. }
  166. }, options);
  167. this.connections[peer] = connection;
  168. break;
  169. case 'PEER_READY':
  170. //if (connection) connection.processQueuedIce();
  171. break;
  172. case 'ANSWER':
  173. if (connection) connection.handleSDP(message);
  174. break;
  175. case 'CANDIDATE':
  176. if (connection) connection.handleCandidate(message);
  177. break;
  178. case 'LEAVE':
  179. if (connection) {
  180. connection.handleLeave();
  181. delete this.connections[peer];
  182. }
  183. break;
  184. case 'PORT':
  185. if (util.browserisms === 'Firefox') {
  186. connection.handlePort(message);
  187. break;
  188. }
  189. case 'DEFAULT':
  190. util.log('PEER: unrecognized message ', message.type);
  191. break;
  192. }
  193. };
  194. /** Process queued calls to connect. */
  195. Peer.prototype._processQueue = function() {
  196. while (this._queued.length > 0) {
  197. var cdata = this._queued.pop();
  198. this.connect.apply(this, cdata);
  199. }
  200. };
  201. Peer.prototype._cleanup = function() {
  202. this._socket.send(JSON.stringify({ type: 'LEAVE', src: this._id }));
  203. for (var peer in this.connections) {
  204. if (this.connections.hasOwnProperty(peer)) {
  205. this.connections[peer].close();
  206. }
  207. }
  208. };
  209. /** Exposed connect function for users. Will try to connect later if user
  210. * is waiting for an ID. */
  211. Peer.prototype.connect = function(peer, metadata, cb) {
  212. if (typeof metadata === 'function' && !cb) cb = metadata; metadata = false;
  213. if (!this._id) {
  214. this._queued.push(Array.prototype.slice.apply(arguments));
  215. return;
  216. }
  217. var options = {
  218. metadata: metadata,
  219. socketOpen: this._socketOpen,
  220. config: this._config
  221. };
  222. var connection = new DataConnection(this._id, peer, this._socket, this._httpUrl, cb, options);
  223. this.connections[peer] = connection;
  224. };
  225. Peer.prototype.destroy = function() {
  226. this._cleanup();
  227. };
  228. exports.Peer = Peer;