peer.js 7.6 KB

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