|
@@ -721,7 +721,7 @@ EventEmitter.prototype.emit = function(type) {
|
|
var util = {
|
|
var util = {
|
|
|
|
|
|
chromeCompatible: true,
|
|
chromeCompatible: true,
|
|
- firefoxCompatibile: false,
|
|
|
|
|
|
+ firefoxCompatible: false,
|
|
chromeVersion: 26,
|
|
chromeVersion: 26,
|
|
firefoxVersion: 22,
|
|
firefoxVersion: 22,
|
|
|
|
|
|
@@ -1219,6 +1219,10 @@ function Peer(id, options) {
|
|
return
|
|
return
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ // States.
|
|
|
|
+ this.destroyed = false;
|
|
|
|
+ this.disconnected = false;
|
|
|
|
+
|
|
// Connections for this peer.
|
|
// Connections for this peer.
|
|
this.connections = {};
|
|
this.connections = {};
|
|
// Connection managers.
|
|
// Connection managers.
|
|
@@ -1390,7 +1394,7 @@ Peer.prototype._cleanup = function() {
|
|
this.managers[peers[i]].close();
|
|
this.managers[peers[i]].close();
|
|
}
|
|
}
|
|
util.setZeroTimeout(function(){
|
|
util.setZeroTimeout(function(){
|
|
- self._socket.close();
|
|
|
|
|
|
+ self.disconnect();
|
|
});
|
|
});
|
|
}
|
|
}
|
|
this.emit('close');
|
|
this.emit('close');
|
|
@@ -1400,11 +1404,11 @@ Peer.prototype._cleanup = function() {
|
|
|
|
|
|
/** Exposed connect function for users. Will try to connect later if user
|
|
/** Exposed connect function for users. Will try to connect later if user
|
|
* is waiting for an ID. */
|
|
* is waiting for an ID. */
|
|
-// TODO: pause XHR streaming when not in use and start again when this is
|
|
|
|
-// called.
|
|
|
|
Peer.prototype.connect = function(peer, options) {
|
|
Peer.prototype.connect = function(peer, options) {
|
|
- if (this.destroyed) {
|
|
|
|
- this._abort('peer-destroyed', 'This Peer has been destroyed and is no longer able to make connections.');
|
|
|
|
|
|
+ if (this.disconnected) {
|
|
|
|
+ var err = new Error('This Peer has been disconnected from the server and');
|
|
|
|
+ err.type = 'peer-disconnected';
|
|
|
|
+ this.emit('error', err);
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -1431,6 +1435,12 @@ Peer.prototype.connect = function(peer, options) {
|
|
return connectionInfo[1];
|
|
return connectionInfo[1];
|
|
};
|
|
};
|
|
|
|
|
|
|
|
+/**
|
|
|
|
+ * Destroys the Peer: closes all active connections as well as the connection
|
|
|
|
+ * to the server.
|
|
|
|
+ * Warning: The peer can no longer create or accept connections after being
|
|
|
|
+ * destroyed.
|
|
|
|
+ */
|
|
Peer.prototype.destroy = function() {
|
|
Peer.prototype.destroy = function() {
|
|
if (!this.destroyed) {
|
|
if (!this.destroyed) {
|
|
this._cleanup();
|
|
this._cleanup();
|
|
@@ -1438,6 +1448,19 @@ Peer.prototype.destroy = function() {
|
|
}
|
|
}
|
|
};
|
|
};
|
|
|
|
|
|
|
|
+/**
|
|
|
|
+ * Disconnects the Peer's connection to the PeerServer. Does not close any
|
|
|
|
+ * active connections.
|
|
|
|
+ * Warning: The peer can no longer create or accept connections after being
|
|
|
|
+ * disconnected. It also cannot reconnect to the server.
|
|
|
|
+ */
|
|
|
|
+Peer.prototype.disconnect = function() {
|
|
|
|
+ if (!this.disconnected) {
|
|
|
|
+ this._socket.close();
|
|
|
|
+ this.disconnected = true;
|
|
|
|
+ }
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
|
|
exports.Peer = Peer;
|
|
exports.Peer = Peer;
|
|
/**
|
|
/**
|
|
@@ -1889,6 +1912,8 @@ function Socket(host, port, key, id) {
|
|
|
|
|
|
this._id = id;
|
|
this._id = id;
|
|
var token = util.randomToken();
|
|
var token = util.randomToken();
|
|
|
|
+
|
|
|
|
+ this.disconnected = false;
|
|
|
|
|
|
this._httpUrl = 'http://' + host + ':' + port + '/' + key + '/' + id + '/' + token;
|
|
this._httpUrl = 'http://' + host + ':' + port + '/' + key + '/' + id + '/' + token;
|
|
this._wsUrl = 'ws://' + host + ':' + port + '/peerjs?key='+key+'&id='+id+'&token='+token;
|
|
this._wsUrl = 'ws://' + host + ':' + port + '/peerjs?key='+key+'&id='+id+'&token='+token;
|
|
@@ -2020,8 +2045,17 @@ Socket.prototype._setHTTPTimeout = function() {
|
|
}, 25000);
|
|
}, 25000);
|
|
};
|
|
};
|
|
|
|
|
|
|
|
+
|
|
|
|
+Socket.prototype._wsOpen = function() {
|
|
|
|
+ return !!this._socket && this._socket.readyState == 1;
|
|
|
|
+};
|
|
|
|
+
|
|
/** Exposed send for DC & Peer. */
|
|
/** Exposed send for DC & Peer. */
|
|
Socket.prototype.send = function(data) {
|
|
Socket.prototype.send = function(data) {
|
|
|
|
+ if (this.disconnected) {
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+
|
|
if (!data.type) {
|
|
if (!data.type) {
|
|
this.emit('error', 'Invalid message');
|
|
this.emit('error', 'Invalid message');
|
|
return;
|
|
return;
|
|
@@ -2040,14 +2074,10 @@ Socket.prototype.send = function(data) {
|
|
};
|
|
};
|
|
|
|
|
|
Socket.prototype.close = function() {
|
|
Socket.prototype.close = function() {
|
|
- if (!!this._wsOpen()) {
|
|
|
|
|
|
+ if (!this.disconnected && this._wsOpen()) {
|
|
this._socket.close();
|
|
this._socket.close();
|
|
|
|
+ this.disconnected = true;
|
|
}
|
|
}
|
|
};
|
|
};
|
|
|
|
|
|
-Socket.prototype._wsOpen = function() {
|
|
|
|
- return !!this._socket && this._socket.readyState == 1;
|
|
|
|
-};
|
|
|
|
-
|
|
|
|
-
|
|
|
|
})(this);
|
|
})(this);
|