|
@@ -893,12 +893,12 @@ Peer.prototype._checkIn = function() {
|
|
var response = JSON.parse(http.responseText.split('\n').shift());
|
|
var response = JSON.parse(http.responseText.split('\n').shift());
|
|
if (!!response.id) {
|
|
if (!!response.id) {
|
|
self._id = response.id;
|
|
self._id = response.id;
|
|
- self._socketInit();
|
|
|
|
|
|
+ //self._socketInit();
|
|
self.emit('ready', self._id);
|
|
self.emit('ready', self._id);
|
|
self._processQueue();
|
|
self._processQueue();
|
|
}
|
|
}
|
|
} catch (e) {
|
|
} catch (e) {
|
|
- self._socketInit();
|
|
|
|
|
|
+ //self._socketInit();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
self._handleStream(http, true);
|
|
self._handleStream(http, true);
|
|
@@ -906,11 +906,11 @@ Peer.prototype._checkIn = function() {
|
|
http.send(null);
|
|
http.send(null);
|
|
} catch(e) {
|
|
} catch(e) {
|
|
util.log('XMLHttpRequest not available; defaulting to WebSockets');
|
|
util.log('XMLHttpRequest not available; defaulting to WebSockets');
|
|
- this._socketInit();
|
|
|
|
|
|
+ //this._socketInit();
|
|
}
|
|
}
|
|
} else {
|
|
} else {
|
|
this._startXhrStream();
|
|
this._startXhrStream();
|
|
- this._socketInit();
|
|
|
|
|
|
+ //this._socketInit();
|
|
}
|
|
}
|
|
// TODO: may need to setInterval in case handleStream is not being called
|
|
// TODO: may need to setInterval in case handleStream is not being called
|
|
// enough.
|
|
// enough.
|
|
@@ -926,8 +926,6 @@ Peer.prototype._startXhrStream = function() {
|
|
self._handleStream(http);
|
|
self._handleStream(http);
|
|
};
|
|
};
|
|
http.send(JSON.stringify({ id: this._id }));
|
|
http.send(JSON.stringify({ id: this._id }));
|
|
- // TODO: may need to setInterval in case handleStream is not being called
|
|
|
|
- // enough.
|
|
|
|
} catch(e) {
|
|
} catch(e) {
|
|
util.log('XMLHttpRequest not available; defaulting to WebSockets');
|
|
util.log('XMLHttpRequest not available; defaulting to WebSockets');
|
|
}
|
|
}
|
|
@@ -940,8 +938,6 @@ Peer.prototype._handleStream = function(http, pad) {
|
|
return;
|
|
return;
|
|
} else if (http.readyState == 3 && http.status != 200) {
|
|
} else if (http.readyState == 3 && http.status != 200) {
|
|
return;
|
|
return;
|
|
- } else if (http.readyState == 4 && http.status != 200) {
|
|
|
|
- // Clear setInterval here if using it.
|
|
|
|
}
|
|
}
|
|
|
|
|
|
if (this._index === undefined)
|
|
if (this._index === undefined)
|
|
@@ -952,13 +948,13 @@ Peer.prototype._handleStream = function(http, pad) {
|
|
|
|
|
|
// TODO: handle
|
|
// TODO: handle
|
|
var message = http.responseText.split('\n')[this._index];
|
|
var message = http.responseText.split('\n')[this._index];
|
|
- if (!!message) {
|
|
|
|
|
|
+ if (!!message && http.readyState == 3) {
|
|
this._index += 1;
|
|
this._index += 1;
|
|
this._handleServerMessage(message);
|
|
this._handleServerMessage(message);
|
|
|
|
+ } else if (http.readyState == 4) {
|
|
|
|
+ this._index = 1;
|
|
}
|
|
}
|
|
|
|
|
|
- if (http.readyState == 4 && !this._socketOpen)
|
|
|
|
- this._startXhrStream();
|
|
|
|
};
|
|
};
|
|
|
|
|
|
/** Start up websocket communications. */
|
|
/** Start up websocket communications. */
|
|
@@ -989,64 +985,75 @@ Peer.prototype._socketInit = function() {
|
|
|
|
|
|
|
|
|
|
Peer.prototype._handleServerMessage = function(message) {
|
|
Peer.prototype._handleServerMessage = function(message) {
|
|
- var message;
|
|
|
|
|
|
+ var msg;
|
|
try {
|
|
try {
|
|
- message = JSON.parse(message);
|
|
|
|
|
|
+ msg = JSON.parse(message);
|
|
} catch(e) {
|
|
} catch(e) {
|
|
- util.log('message unrecognizable:', message);
|
|
|
|
|
|
+ switch (message) {
|
|
|
|
+ case 'end':
|
|
|
|
+ util.log('XHR stream timed out.');
|
|
|
|
+ if (!this._socketOpen)
|
|
|
|
+ this._startXhrStream();
|
|
|
|
+ break;
|
|
|
|
+ case 'socket':
|
|
|
|
+ util.log('XHR stream closed, WebSocket connected.');
|
|
|
|
+ break;
|
|
|
|
+ case 'error':
|
|
|
|
+ util.log('Something went wrong.');
|
|
|
|
+ break;
|
|
|
|
+ default:
|
|
|
|
+ util.log('Message unrecognized:', message);
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
- var peer = message.src;
|
|
|
|
|
|
+ var peer = msg.src;
|
|
var connection = this.connections[peer];
|
|
var connection = this.connections[peer];
|
|
- switch (message.type) {
|
|
|
|
|
|
+ switch (msg.type) {
|
|
case 'ID':
|
|
case 'ID':
|
|
if (!this._id) {
|
|
if (!this._id) {
|
|
// If we're just now getting an ID then we may have a queue.
|
|
// If we're just now getting an ID then we may have a queue.
|
|
- this._id = message.id;
|
|
|
|
|
|
+ this._id = msg.id;
|
|
this.emit('ready', this._id);
|
|
this.emit('ready', this._id);
|
|
this._processQueue();
|
|
this._processQueue();
|
|
}
|
|
}
|
|
break;
|
|
break;
|
|
case 'ERROR':
|
|
case 'ERROR':
|
|
- //throw new Error(message.msg);
|
|
|
|
- this.emit('error', message.msg);
|
|
|
|
- util.log(message.msg);
|
|
|
|
|
|
+ this.emit('error', msg.msg);
|
|
|
|
+ util.log(msg.msg);
|
|
break;
|
|
break;
|
|
case 'OFFER':
|
|
case 'OFFER':
|
|
var options = {
|
|
var options = {
|
|
- metadata: message.metadata,
|
|
|
|
- sdp: message.sdp,
|
|
|
|
|
|
+ metadata: msg.metadata,
|
|
|
|
+ sdp: msg.sdp,
|
|
socketOpen: this._socketOpen,
|
|
socketOpen: this._socketOpen,
|
|
config: this._config
|
|
config: this._config
|
|
};
|
|
};
|
|
var self = this;
|
|
var self = this;
|
|
var connection = new DataConnection(this._id, peer, this._socket, this._httpUrl, function(err, connection) {
|
|
var connection = new DataConnection(this._id, peer, this._socket, this._httpUrl, function(err, connection) {
|
|
if (!err) {
|
|
if (!err) {
|
|
- self.emit('connection', connection, message.metadata);
|
|
|
|
|
|
+ self.emit('connection', connection, msg.metadata);
|
|
}
|
|
}
|
|
}, options);
|
|
}, options);
|
|
this._attachConnectionListeners(connection);
|
|
this._attachConnectionListeners(connection);
|
|
this.connections[peer] = connection;
|
|
this.connections[peer] = connection;
|
|
break;
|
|
break;
|
|
- case 'PEER_READY':
|
|
|
|
- //if (connection) connection.processQueuedIce();
|
|
|
|
- break;
|
|
|
|
case 'ANSWER':
|
|
case 'ANSWER':
|
|
- if (connection) connection.handleSDP(message);
|
|
|
|
|
|
+ if (connection) connection.handleSDP(msg);
|
|
break;
|
|
break;
|
|
case 'CANDIDATE':
|
|
case 'CANDIDATE':
|
|
- if (connection) connection.handleCandidate(message);
|
|
|
|
|
|
+ if (connection) connection.handleCandidate(msg);
|
|
break;
|
|
break;
|
|
case 'LEAVE':
|
|
case 'LEAVE':
|
|
if (connection) connection.handleLeave();
|
|
if (connection) connection.handleLeave();
|
|
break;
|
|
break;
|
|
case 'PORT':
|
|
case 'PORT':
|
|
if (util.browserisms === 'Firefox') {
|
|
if (util.browserisms === 'Firefox') {
|
|
- connection.handlePort(message);
|
|
|
|
|
|
+ connection.handlePort(msg);
|
|
break;
|
|
break;
|
|
}
|
|
}
|
|
case 'DEFAULT':
|
|
case 'DEFAULT':
|
|
- util.log('PEER: unrecognized message ', message.type);
|
|
|
|
|
|
+ util.log('Unrecognized message type:', msg.type);
|
|
break;
|
|
break;
|
|
}
|
|
}
|
|
};
|
|
};
|
|
@@ -1061,7 +1068,15 @@ Peer.prototype._processQueue = function() {
|
|
|
|
|
|
|
|
|
|
Peer.prototype._cleanup = function() {
|
|
Peer.prototype._cleanup = function() {
|
|
- this._socket.send(JSON.stringify({ type: 'LEAVE', src: this._id }));
|
|
|
|
|
|
+ if (this._socketOpen) {
|
|
|
|
+ this._socket.send(JSON.stringify({ type: 'LEAVE', src: this._id }));
|
|
|
|
+ } else {
|
|
|
|
+ var http = new XMLHttpRequest();
|
|
|
|
+ http.open('post', this._httpUrl + '/leave', true);
|
|
|
|
+ http.setRequestHeader('Content-Type', 'application/json');
|
|
|
|
+ http.send(JSON.stringify({ type: 'LEAVE', src: this._id }));
|
|
|
|
+ }
|
|
|
|
+
|
|
for (var peer in this.connections) {
|
|
for (var peer in this.connections) {
|
|
if (this.connections.hasOwnProperty(peer)) {
|
|
if (this.connections.hasOwnProperty(peer)) {
|
|
this.connections[peer].close();
|
|
this.connections[peer].close();
|
|
@@ -1122,7 +1137,6 @@ function DataConnection(id, peer, socket, httpUrl, cb, options) {
|
|
this._originator = (options.sdp === undefined);
|
|
this._originator = (options.sdp === undefined);
|
|
this._cb = cb;
|
|
this._cb = cb;
|
|
this._httpUrl = httpUrl;
|
|
this._httpUrl = httpUrl;
|
|
- //this._peerReady = true;
|
|
|
|
this.metadata = options.metadata;
|
|
this.metadata = options.metadata;
|
|
this._socketOpen = options.socketOpen;
|
|
this._socketOpen = options.socketOpen;
|
|
this._config = options.config;
|
|
this._config = options.config;
|
|
@@ -1134,13 +1148,11 @@ function DataConnection(id, peer, socket, httpUrl, cb, options) {
|
|
if (util.browserisms === 'Firefox') {
|
|
if (util.browserisms === 'Firefox') {
|
|
this._firefoxPortSetup();
|
|
this._firefoxPortSetup();
|
|
}
|
|
}
|
|
- //
|
|
|
|
|
|
|
|
// Set up PeerConnection.
|
|
// Set up PeerConnection.
|
|
this._startPeerConnection();
|
|
this._startPeerConnection();
|
|
|
|
|
|
// Listen for ICE candidates
|
|
// Listen for ICE candidates
|
|
- //this._queuedIce = [];
|
|
|
|
this._setupIce();
|
|
this._setupIce();
|
|
|
|
|
|
// Listen for negotiation needed
|
|
// Listen for negotiation needed
|
|
@@ -1213,17 +1225,6 @@ DataConnection.prototype._setupIce = function() {
|
|
dst: self._peer,
|
|
dst: self._peer,
|
|
src: self._id
|
|
src: self._id
|
|
}));
|
|
}));
|
|
- /*var data = JSON.stringify({
|
|
|
|
- type: 'CANDIDATE',
|
|
|
|
- candidate: evt.candidate,
|
|
|
|
- dst: self._peer,
|
|
|
|
- src: self._id
|
|
|
|
- });
|
|
|
|
- if (self._peerReady) {
|
|
|
|
- self._handleBroker('ice', data);
|
|
|
|
- } else {
|
|
|
|
- self._queuedIce.push(data);
|
|
|
|
- }*/
|
|
|
|
}
|
|
}
|
|
};
|
|
};
|
|
};
|
|
};
|
|
@@ -1241,27 +1242,12 @@ DataConnection.prototype._handleBroker = function(type, data) {
|
|
if (http.responseText != 'OK') {
|
|
if (http.responseText != 'OK') {
|
|
util.log('Destination peer not available. Connection closing...');
|
|
util.log('Destination peer not available. Connection closing...');
|
|
self.close();
|
|
self.close();
|
|
- }/* else {
|
|
|
|
- if (type == 'offer') {
|
|
|
|
- self.processQueuedIce();
|
|
|
|
- }
|
|
|
|
- }*/
|
|
|
|
|
|
+ }
|
|
}
|
|
}
|
|
http.send(data);
|
|
http.send(data);
|
|
}
|
|
}
|
|
};
|
|
};
|
|
|
|
|
|
-/*
|
|
|
|
-DataConnection.prototype.processQueuedIce = function() {
|
|
|
|
- this._peerReady = true;
|
|
|
|
- console.log('processing ice');
|
|
|
|
- while (this._queuedIce.length > 0) {
|
|
|
|
- var data = this._queuedIce.shift();
|
|
|
|
- this._handleBroker('ice', data);
|
|
|
|
- }
|
|
|
|
-}
|
|
|
|
-*/
|
|
|
|
-
|
|
|
|
// Awaiting update in Firefox spec ***
|
|
// Awaiting update in Firefox spec ***
|
|
/** Sets up DataChannel handlers.
|
|
/** Sets up DataChannel handlers.
|
|
DataConnection.prototype._setupDataChannel = function() {
|
|
DataConnection.prototype._setupDataChannel = function() {
|
|
@@ -1427,8 +1413,6 @@ DataConnection.prototype._handleDataMessage = function(e) {
|
|
|
|
|
|
/** Allows user to close connection. */
|
|
/** Allows user to close connection. */
|
|
DataConnection.prototype.close = function() {
|
|
DataConnection.prototype.close = function() {
|
|
- // TODO: how to emit close to the Peer? Also, how to handle Websocket closing
|
|
|
|
- // gracefully?
|
|
|
|
this._cleanup();
|
|
this._cleanup();
|
|
var self = this;
|
|
var self = this;
|
|
this._handleBroker('leave', JSON.stringify({
|
|
this._handleBroker('leave', JSON.stringify({
|
|
@@ -1436,7 +1420,6 @@ DataConnection.prototype.close = function() {
|
|
dst: self._peer,
|
|
dst: self._peer,
|
|
src: self._id,
|
|
src: self._id,
|
|
}));
|
|
}));
|
|
- this.emit('close', this._peer);
|
|
|
|
};
|
|
};
|
|
|
|
|
|
|
|
|