|
@@ -17,16 +17,12 @@ function Peer(options) {
|
|
|
throw new Error('Peer ID can only contain alphanumerics, "_", and "-".');
|
|
|
|
|
|
this._id = options.id;
|
|
|
- // If no ID provided, get a unique ID from server. TODO
|
|
|
-
|
|
|
- if (!this._id) {
|
|
|
// Not used unless using cloud server.
|
|
|
this._apikey = options.apikey;
|
|
|
|
|
|
-
|
|
|
- // Must have an ID at this point.
|
|
|
- this._socket = new WebSocket('ws://' + this._server + '/ws?');
|
|
|
- this._socketInit();
|
|
|
+ // Check in with the server with ID or get an ID.
|
|
|
+// this._startXhrStream();
|
|
|
+ this._checkIn();
|
|
|
|
|
|
// Connections for this peer.
|
|
|
this.connections = {};
|
|
@@ -40,14 +36,105 @@ function Peer(options) {
|
|
|
|
|
|
util.inherits(Peer, EventEmitter);
|
|
|
|
|
|
+/** Check in with ID or get one from server. */
|
|
|
+Peer.prototype._checkIn = function() {
|
|
|
+ try {
|
|
|
+ var http = new XMLHttpRequest();
|
|
|
+ // If no ID provided, get a unique ID from server.
|
|
|
+ var self = this;
|
|
|
+ if (!this._id) {
|
|
|
+ // If there's no ID we need to wait for one before trying to init socket.
|
|
|
+ http.open('get', this._server + '/id', true);
|
|
|
+ http.onreadystatechange = function() {
|
|
|
+ if (http.readyState > 2) {
|
|
|
+ if (!!http.responseText) {
|
|
|
+ try {
|
|
|
+ var response = JSON.parse(http.responseText);
|
|
|
+ if (!!response.id) {
|
|
|
+ self._id = response.id;
|
|
|
+ }
|
|
|
+ } catch (e) {
|
|
|
+ // Ignore
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ self._socketInit();
|
|
|
+ self._handleStream(http);
|
|
|
+ };
|
|
|
+ http.send(null);
|
|
|
+ } else {
|
|
|
+ this._socketInit();
|
|
|
+ http.open('post', this._server + '/id', true);
|
|
|
+ http.onreadystatechange = function() {
|
|
|
+ self._handleStream(http);
|
|
|
+ };
|
|
|
+ http.send('id=' + this._id);
|
|
|
+ }
|
|
|
+ // TODO: may need to setInterval in case handleStream is not being called
|
|
|
+ // enough.
|
|
|
+ } catch(e) {
|
|
|
+ util.log('XMLHttpRequest not available; defaulting to WebSockets');
|
|
|
+ this._socketInit();
|
|
|
+ }
|
|
|
+};
|
|
|
+
|
|
|
+/** Taking care of this in /id now.
|
|
|
+Peer.prototype._startXhrStream = function() {
|
|
|
+ try {
|
|
|
+ var http = new XMLHttpRequest();
|
|
|
+ // If no ID provided, get a unique ID from server.
|
|
|
+ var self = this;
|
|
|
+ http.open('get', this._server + '/stream', true);
|
|
|
+ http.onreadystatechange = function() {
|
|
|
+ self._handleStream(http);
|
|
|
+ };
|
|
|
+ http.send(null);
|
|
|
+ // TODO: may need to setInterval in case handleStream is not being called
|
|
|
+ // enough.
|
|
|
+ } catch(e) {
|
|
|
+ util.log('XMLHttpRequest not available; defaulting to WebSockets');
|
|
|
+ }
|
|
|
+};
|
|
|
+*/
|
|
|
+
|
|
|
+/** Handles onreadystatechange response as a stream. */
|
|
|
+Peer.prototype._handleStream = function(http) {
|
|
|
+ // 3 and 4 are loading/done state. All others are not relevant.
|
|
|
+ if (http.readyState < 3) {
|
|
|
+ return;
|
|
|
+ } else if (http.readyState == 3 && http.status != 200) {
|
|
|
+ return;
|
|
|
+ } else if (http.readyState == 4 && http.status != 200) {
|
|
|
+ // Clear setInterval here if using it.
|
|
|
+ }
|
|
|
+
|
|
|
+ if (http.responseText === null)
|
|
|
+ return;
|
|
|
+
|
|
|
+ // TODO: handle
|
|
|
+ console.log(http.responseText);
|
|
|
+
|
|
|
+ if (http.readyState == 4 && !this._socketOpen)
|
|
|
+ this._startXhrStream();
|
|
|
+};
|
|
|
+
|
|
|
/** Start up websocket communications. */
|
|
|
Peer.prototype._socketInit = function() {
|
|
|
+ this._socket = new WebSocket('ws://' + this._server + '/ws?id=' + this._id);
|
|
|
+
|
|
|
var self = this;
|
|
|
this._socket.onmessage = function(event) {
|
|
|
var message = JSON.parse(event.data);
|
|
|
var peer = message.src;
|
|
|
var connection = self.connections[peer];
|
|
|
switch (message.type) {
|
|
|
+ case 'ID':
|
|
|
+ if (!self._id) {
|
|
|
+ // If we're just now getting an ID then we may have a queue.
|
|
|
+ self._id = message.id;
|
|
|
+ self._processQueue();
|
|
|
+ }
|
|
|
+ break;
|
|
|
case 'OFFER':
|
|
|
var options = {
|
|
|
metadata: message.metadata,
|
|
@@ -79,8 +166,13 @@ Peer.prototype._socketInit = function() {
|
|
|
break;
|
|
|
}
|
|
|
};
|
|
|
+
|
|
|
+ // Take care of the queue of connections if necessary and make sure Peer knows
|
|
|
+ // socket is open.
|
|
|
this._socket.onopen = function() {
|
|
|
- self._processQueue();
|
|
|
+ self._socketOpen = true;
|
|
|
+ if (self._id)
|
|
|
+ self._processQueue();
|
|
|
};
|
|
|
};
|
|
|
|