|
@@ -24,7 +24,7 @@ function Peer(options) {
|
|
|
throw new Error('API key can only contain alphanumerics, "_", and "-".');
|
|
|
}
|
|
|
|
|
|
- this._id = options.id;
|
|
|
+ this.id = options.id;
|
|
|
// Not used unless using cloud server.
|
|
|
this._key = options.key;
|
|
|
|
|
@@ -41,21 +41,22 @@ util.inherits(Peer, EventEmitter);
|
|
|
|
|
|
Peer.prototype._startSocket = function() {
|
|
|
var self = this;
|
|
|
- this._socket = new Socket(this._server, this._id, this._key);
|
|
|
+ this._socket = new Socket(this._server, this.id, this._key);
|
|
|
this._socket.on('message', function(data) {
|
|
|
self._handleServerJSONMessage(data);
|
|
|
});
|
|
|
this._socket.on('open', function() {
|
|
|
self._processQueue();
|
|
|
});
|
|
|
- this._socket.on('unavailable', function(peer) {
|
|
|
- util.log('Destination peer not available.', peer);
|
|
|
- if (self.connections[peer]) {
|
|
|
- self.connections[peer].close();
|
|
|
- }
|
|
|
- });
|
|
|
this._socket.on('error', function(error) {
|
|
|
util.log(error);
|
|
|
+ self.emit('error', error);
|
|
|
+ });
|
|
|
+ this._socket.on('close', function() {
|
|
|
+ var msg = 'Underlying socket has closed';
|
|
|
+ util.log('error', msg);
|
|
|
+ self.emit('error', msg);
|
|
|
+ self.emit('close');
|
|
|
});
|
|
|
this._socket.start();
|
|
|
}
|
|
@@ -66,10 +67,10 @@ Peer.prototype._handleServerJSONMessage = function(message) {
|
|
|
var connection = this.connections[peer];
|
|
|
switch (message.type) {
|
|
|
case 'ID':
|
|
|
- if (!this._id) {
|
|
|
+ if (!this.id) {
|
|
|
// If we're just now getting an ID then we may have a queue.
|
|
|
- this._id = message.id;
|
|
|
- this.emit('ready', this._id);
|
|
|
+ this.id = message.id;
|
|
|
+ this.emit('open', this.id);
|
|
|
this._processQueue();
|
|
|
}
|
|
|
break;
|
|
@@ -79,7 +80,8 @@ Peer.prototype._handleServerJSONMessage = function(message) {
|
|
|
break;
|
|
|
case 'ID-TAKEN':
|
|
|
this.emit('error', message.msg);
|
|
|
- this.destroy('ID `'+this._id+'` is taken');
|
|
|
+ this.destroy('ID `'+this.id+'` is taken');
|
|
|
+ this.emit('close');
|
|
|
break;
|
|
|
case 'OFFER':
|
|
|
var options = {
|
|
@@ -88,7 +90,7 @@ Peer.prototype._handleServerJSONMessage = function(message) {
|
|
|
config: this._options.config,
|
|
|
};
|
|
|
var self = this;
|
|
|
- var connection = new DataConnection(this._id, peer, this._socket, function(err, connection) {
|
|
|
+ var connection = new DataConnection(this.id, peer, this._socket, function(err, connection) {
|
|
|
if (!err) {
|
|
|
self.emit('connection', connection, message.metadata);
|
|
|
}
|
|
@@ -98,7 +100,7 @@ Peer.prototype._handleServerJSONMessage = function(message) {
|
|
|
break;
|
|
|
case 'EXPIRE':
|
|
|
if (connection) {
|
|
|
- connection.close('Could not connect to peer ' + connection._peer);
|
|
|
+ connection.close('Could not connect to peer ' + connection.peer);
|
|
|
}
|
|
|
break;
|
|
|
case 'ANSWER':
|
|
@@ -166,7 +168,7 @@ Peer.prototype._attachConnectionListeners = function(connection) {
|
|
|
Peer.prototype.connect = function(peer, metadata, cb) {
|
|
|
if (typeof metadata === 'function' && !cb) cb = metadata; metadata = false;
|
|
|
|
|
|
- if (!this._id) {
|
|
|
+ if (!this.id) {
|
|
|
this._queued.push(Array.prototype.slice.apply(arguments));
|
|
|
return;
|
|
|
}
|
|
@@ -175,7 +177,7 @@ Peer.prototype.connect = function(peer, metadata, cb) {
|
|
|
metadata: metadata,
|
|
|
config: this._options.config,
|
|
|
};
|
|
|
- var connection = new DataConnection(this._id, peer, this._socket, cb, options);
|
|
|
+ var connection = new DataConnection(this.id, peer, this._socket, cb, options);
|
|
|
this._attachConnectionListeners(connection);
|
|
|
|
|
|
this.connections[peer] = connection;
|