|
@@ -66,7 +66,8 @@ function Peer(id, options) {
|
|
|
|
|
|
// States.
|
|
|
this.destroyed = false; // Connections have been killed
|
|
|
- this.disconnected = false; // Connection to PeerServer killed but P2P connections still active
|
|
|
+ this.disconnected = false; // Connection to PeerServer killed manually but P2P connections still active
|
|
|
+ this.open = false; // Sockets and such are not yet open.
|
|
|
//
|
|
|
|
|
|
// References
|
|
@@ -130,7 +131,7 @@ Peer.prototype._initialize = function(id) {
|
|
|
});
|
|
|
this.socket.on('close', function() {
|
|
|
if (!self.disconnected) { // If we haven't explicitly disconnected, emit error.
|
|
|
- self._abort('socket-closed', 'Underlying socket has closed');
|
|
|
+ self._abort('socket-closed', 'Underlying socket is already closed.');
|
|
|
}
|
|
|
});
|
|
|
this.socket.start();
|
|
@@ -145,6 +146,7 @@ Peer.prototype._handleMessage = function(message) {
|
|
|
switch (type) {
|
|
|
case 'OPEN': // The connection to the server is open.
|
|
|
this.emit('open', this.id);
|
|
|
+ this.open = true;
|
|
|
break;
|
|
|
case 'ERROR': // Server error.
|
|
|
this._abort('server-error', payload.msg);
|
|
@@ -318,6 +320,7 @@ Peer.prototype.disconnect = function() {
|
|
|
util.setZeroTimeout(function(){
|
|
|
if (!self.disconnected) {
|
|
|
self.disconnected = true;
|
|
|
+ self.open = false;
|
|
|
if (self.socket) {
|
|
|
self.socket.close();
|
|
|
}
|