|
@@ -9,7 +9,7 @@ function Peer(id, options) {
|
|
|
if (id && id.constructor == Object) {
|
|
|
options = id;
|
|
|
id = undefined;
|
|
|
- } else {
|
|
|
+ } else if (id) {
|
|
|
// Ensure id is a string
|
|
|
id = id.toString();
|
|
|
}
|
|
@@ -30,7 +30,7 @@ function Peer(id, options) {
|
|
|
}
|
|
|
// Set whether we use SSL to same as current host
|
|
|
if (options.secure === undefined) {
|
|
|
- options.secure = util.isSecure();
|
|
|
+ options.secure = util.isSecure();
|
|
|
}
|
|
|
// TODO: document this feature
|
|
|
// Set a custom log function if present
|
|
@@ -85,13 +85,16 @@ function Peer(id, options) {
|
|
|
|
|
|
util.inherits(Peer, EventEmitter);
|
|
|
|
|
|
+/** Get a unique ID from the server via XHR. */
|
|
|
Peer.prototype._retrieveId = function(cb) {
|
|
|
var self = this;
|
|
|
var http = new XMLHttpRequest();
|
|
|
+ // TODO: apparently using ://something.com gives relative protocol?
|
|
|
var protocol = this.options.secure ? 'https://' : 'http://';
|
|
|
var url = protocol + this.options.host + ':' + this.options.port + '/' + this.options.key + '/id';
|
|
|
var queryString = '?ts=' + new Date().getTime() + '' + Math.random();
|
|
|
url += queryString;
|
|
|
+
|
|
|
// If there's no ID we need to wait for one before trying to init socket.
|
|
|
http.open('get', url, true);
|
|
|
http.onerror = function(e) {
|
|
@@ -111,20 +114,24 @@ Peer.prototype._retrieveId = function(cb) {
|
|
|
http.send(null);
|
|
|
};
|
|
|
|
|
|
-
|
|
|
+/** Initialize a connection with the server. */
|
|
|
Peer.prototype._initialize = function(id) {
|
|
|
var self = this;
|
|
|
this.id = id;
|
|
|
+
|
|
|
+ // Initialize the 'socket' (which is actually a mix of XHR streaming and
|
|
|
+ // websockets.
|
|
|
this.socket = new Socket(this.options.secure, this.options.host, this.options.port, this.options.key, this.id);
|
|
|
- this.socket.on('server-message', function(data) {
|
|
|
+ this.socket.on('message', function(data) {
|
|
|
self._handleMessage(data);
|
|
|
});
|
|
|
this.socket.on('error', function(error) {
|
|
|
self._abort('socket-error', error);
|
|
|
});
|
|
|
this.socket.on('close', function() {
|
|
|
- // TODO: What if we disconnected on purpose?
|
|
|
- self._abort('socket-closed', 'Underlying socket has closed');
|
|
|
+ if (!self.disconnected) { // If we haven't explicitly disconnected, emit error.
|
|
|
+ self._abort('socket-closed', 'Underlying socket has closed');
|
|
|
+ }
|
|
|
});
|
|
|
this.socket.start();
|
|
|
}
|
|
@@ -169,7 +176,8 @@ Peer.prototype._handleMessage = function(message) {
|
|
|
var connection = new DataConnection(peer, {
|
|
|
id: id,
|
|
|
offer: offer,
|
|
|
- sdp: payload.sdp
|
|
|
+ sdp: payload.sdp,
|
|
|
+ config: this.options.config
|
|
|
});
|
|
|
this._addConnection(peer, connection);
|
|
|
this.emit('connection', connection);
|
|
@@ -282,11 +290,11 @@ Peer.prototype.disconnect = function() {
|
|
|
var self = this;
|
|
|
util.setZeroTimeout(function(){
|
|
|
if (!self.disconnected) {
|
|
|
+ self.disconnected = true;
|
|
|
if (self.socket) {
|
|
|
self.socket.close();
|
|
|
}
|
|
|
self.id = null;
|
|
|
- self.disconnected = true;
|
|
|
}
|
|
|
});
|
|
|
}
|