peer.js 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  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. };
  29. util.inherits(Peer, EventEmitter);
  30. /** Check in with ID or get one from server. */
  31. Peer.prototype._checkIn = function() {
  32. // If no ID provided, get a unique ID from server.
  33. var self = this;
  34. if (!this._id) {
  35. try {
  36. var http = new XMLHttpRequest();
  37. // If there's no ID we need to wait for one before trying to init socket.
  38. http.open('get', this._httpUrl + '/id', true);
  39. http.onreadystatechange = function() {
  40. if (!self._id && http.readyState > 2 && !!http.responseText) {
  41. try {
  42. var response = JSON.parse(http.responseText.split('\n').shift());
  43. if (!!response.id) {
  44. self._id = response.id;
  45. self._socketInit();
  46. self.emit('ready', self._id);
  47. self._processQueue();
  48. }
  49. } catch (e) {
  50. self._socketInit();
  51. }
  52. }
  53. self._handleStream(http, true);
  54. };
  55. http.send(null);
  56. } catch(e) {
  57. util.log('XMLHttpRequest not available; defaulting to WebSockets');
  58. this._socketInit();
  59. }
  60. } else {
  61. this._startXhrStream();
  62. this._socketInit();
  63. }
  64. // TODO: may need to setInterval in case handleStream is not being called
  65. // enough.
  66. };
  67. Peer.prototype._startXhrStream = function() {
  68. try {
  69. var http = new XMLHttpRequest();
  70. var self = this;
  71. http.open('post', this._httpUrl + '/id', true);
  72. http.setRequestHeader('Content-Type', 'application/json');
  73. http.onreadystatechange = function() {
  74. self._handleStream(http);
  75. };
  76. http.send(JSON.stringify({ id: this._id }));
  77. } catch(e) {
  78. util.log('XMLHttpRequest not available; defaulting to WebSockets');
  79. }
  80. };
  81. /** Handles onreadystatechange response as a stream. */
  82. Peer.prototype._handleStream = function(http, pad) {
  83. // 3 and 4 are loading/done state. All others are not relevant.
  84. if (http.readyState < 3) {
  85. return;
  86. } else if (http.readyState == 3 && http.status != 200) {
  87. return;
  88. }
  89. if (this._index === undefined)
  90. this._index = pad ? 2 : 1;
  91. if (http.responseText === null)
  92. return;
  93. // TODO: handle
  94. var message = http.responseText.split('\n')[this._index];
  95. if (!!message && http.readyState == 3) {
  96. this._index += 1;
  97. this._handleServerMessage(message);
  98. } else if (http.readyState == 4) {
  99. this._index = 1;
  100. }
  101. };
  102. /** Start up websocket communications. */
  103. Peer.prototype._socketInit = function() {
  104. if (!!this._socket)
  105. return;
  106. var wsurl = 'ws://' + this._server + '/ws';
  107. if (!!this._id)
  108. wsurl += '?id=' + this._id;
  109. this._socket = new WebSocket(wsurl);
  110. var self = this;
  111. this._socket.onmessage = function(event) {
  112. self._handleServerMessage(event.data);
  113. };
  114. // Take care of the queue of connections if necessary and make sure Peer knows
  115. // socket is open.
  116. this._socket.onopen = function() {
  117. util.log('Socket open');
  118. self._socketOpen = true;
  119. var ids = Object.keys(self.connections)
  120. for (var i = 0, ii = ids.length; i < ii; i += 1) {
  121. self.connections[ids[i]].setSocketOpen();
  122. }
  123. if (self._id)
  124. self._processQueue();
  125. };
  126. };
  127. Peer.prototype._handleServerMessage = function(message) {
  128. message = JSON.parse(message);
  129. var peer = message.src;
  130. var connection = this.connections[peer];
  131. switch (message.type) {
  132. // XHR stream closed by timeout.
  133. case 'HTTP-END':
  134. util.log('XHR stream timed out.');
  135. if (!this._socketOpen)
  136. this._startXhrStream();
  137. break;
  138. // XHR stream closed by socket connect.
  139. case 'HTTP-SOCKET':
  140. util.log('XHR stream closed, WebSocket connected.');
  141. break;
  142. case 'HTTP-ERROR':
  143. util.log('Something went wrong.');
  144. break;
  145. case 'ID':
  146. if (!this._id) {
  147. // If we're just now getting an ID then we may have a queue.
  148. this._id = message.id;
  149. this.emit('ready', this._id);
  150. this._processQueue();
  151. }
  152. break;
  153. case 'ERROR':
  154. this.emit('error', message.msg);
  155. util.log(message.msg);
  156. break;
  157. case 'OFFER':
  158. var options = {
  159. metadata: message.metadata,
  160. sdp: message.sdp,
  161. socketOpen: this._socketOpen,
  162. config: this._config
  163. };
  164. var self = this;
  165. var connection = new DataConnection(this._id, peer, this._socket, this._httpUrl, function(err, connection) {
  166. if (!err) {
  167. self.emit('connection', connection, message.metadata);
  168. }
  169. }, options);
  170. this._attachConnectionListeners(connection);
  171. this.connections[peer] = connection;
  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) connection.handleLeave();
  181. break;
  182. case 'PORT':
  183. if (util.browserisms === 'Firefox') {
  184. connection.handlePort(message);
  185. break;
  186. }
  187. case 'DEFAULT':
  188. util.log('Unrecognized message type:', message.type);
  189. break;
  190. }
  191. };
  192. /** Process queued calls to connect. */
  193. Peer.prototype._processQueue = function() {
  194. while (this._queued.length > 0) {
  195. var cdata = this._queued.pop();
  196. this.connect.apply(this, cdata);
  197. }
  198. };
  199. Peer.prototype._cleanup = function() {
  200. for (var peer in this.connections) {
  201. if (this.connections.hasOwnProperty(peer)) {
  202. this.connections[peer].close();
  203. }
  204. }
  205. if (this._socketOpen)
  206. this._socket.close();
  207. };
  208. /** Listeners for DataConnection events. */
  209. Peer.prototype._attachConnectionListeners = function(connection) {
  210. var self = this;
  211. connection.on('close', function(peer) {
  212. if (self.connections[peer]) delete self.connections[peer];
  213. });
  214. };
  215. /** Exposed connect function for users. Will try to connect later if user
  216. * is waiting for an ID. */
  217. // TODO: pause XHR streaming when not in use and start again when this is
  218. // called.
  219. Peer.prototype.connect = function(peer, metadata, cb) {
  220. if (typeof metadata === 'function' && !cb) cb = metadata; metadata = false;
  221. if (!this._id) {
  222. this._queued.push(Array.prototype.slice.apply(arguments));
  223. return;
  224. }
  225. var options = {
  226. metadata: metadata,
  227. socketOpen: this._socketOpen,
  228. config: this._config
  229. };
  230. var connection = new DataConnection(this._id, peer, this._socket, this._httpUrl, cb, options);
  231. this._attachConnectionListeners(connection);
  232. this.connections[peer] = connection;
  233. };
  234. Peer.prototype.destroy = function() {
  235. this._cleanup();
  236. };
  237. exports.Peer = Peer;