|
@@ -1,7 +1,7 @@
|
|
|
/**
|
|
|
* A DataChannel PeerConnection between two Peers.
|
|
|
*/
|
|
|
-function DataConnection(id, peer, socket, cb, options) {
|
|
|
+function DataConnection(id, peer, socket, options) {
|
|
|
if (!(this instanceof DataConnection)) return new DataConnection(options);
|
|
|
EventEmitter.call(this);
|
|
|
|
|
@@ -16,10 +16,9 @@ function DataConnection(id, peer, socket, cb, options) {
|
|
|
|
|
|
this.id = id;
|
|
|
this.peer = peer;
|
|
|
- this._originator = (options.sdp === undefined);
|
|
|
- this._cb = cb;
|
|
|
this.metadata = options.metadata;
|
|
|
|
|
|
+ this._originator = (options.sdp === undefined);
|
|
|
this._socket = socket;
|
|
|
|
|
|
// Firefoxism: connectDataConnection ports.
|
|
@@ -133,7 +132,6 @@ DataConnection.prototype._configureDataChannel = function() {
|
|
|
util.log('Data channel connection success');
|
|
|
self.open = true;
|
|
|
self.emit('open');
|
|
|
- self._callback(null, self);
|
|
|
};
|
|
|
this._dc.onmessage = function(e) {
|
|
|
self._handleDataMessage(e);
|
|
@@ -166,7 +164,7 @@ DataConnection.prototype._makeOffer = function() {
|
|
|
metadata: self.metadata
|
|
|
});
|
|
|
}, function(err) {
|
|
|
- self._callback('Failed to setLocalDescription');
|
|
|
+ self.emit('error', 'Failed to setLocalDescription');
|
|
|
util.log('Failed to setLocalDescription, ', err);
|
|
|
});
|
|
|
});
|
|
@@ -186,11 +184,11 @@ DataConnection.prototype._makeAnswer = function() {
|
|
|
dst: self.peer
|
|
|
});
|
|
|
}, function(err) {
|
|
|
- self._callback('Failed to setLocalDescription');
|
|
|
+ self.emit('error', 'Failed to setLocalDescription');
|
|
|
util.log('Failed to setLocalDescription, ', err)
|
|
|
});
|
|
|
}, function(err) {
|
|
|
- self._callback('Failed to create answer');
|
|
|
+ self.emit('error', 'Failed to create answer');
|
|
|
util.log('Failed to create answer, ', err)
|
|
|
});
|
|
|
};
|
|
@@ -205,17 +203,8 @@ DataConnection.prototype._cleanup = function() {
|
|
|
this._pc.close();
|
|
|
this._pc = null;
|
|
|
}
|
|
|
- this.emit('close', this.peer);
|
|
|
};
|
|
|
|
|
|
-// Make sure _cb only gets called once
|
|
|
-DataConnection.prototype._callback = function(err, dc) {
|
|
|
- if (this._cb) {
|
|
|
- this._cb(err, dc);
|
|
|
- delete this._cb;
|
|
|
- }
|
|
|
-}
|
|
|
-
|
|
|
|
|
|
// Handles a DataChannel message.
|
|
|
DataConnection.prototype._handleDataMessage = function(e) {
|
|
@@ -241,7 +230,7 @@ DataConnection.prototype._handleDataMessage = function(e) {
|
|
|
*/
|
|
|
|
|
|
/** Allows user to close connection. */
|
|
|
-DataConnection.prototype.close = function(reason) {
|
|
|
+DataConnection.prototype.close = function() {
|
|
|
this._cleanup();
|
|
|
var self = this;
|
|
|
if (this.open) {
|
|
@@ -250,10 +239,9 @@ DataConnection.prototype.close = function(reason) {
|
|
|
dst: self.peer,
|
|
|
src: self.id,
|
|
|
});
|
|
|
- } else {
|
|
|
- this._callback(reason);
|
|
|
}
|
|
|
this.open = false;
|
|
|
+ this.emit('close', this.peer);
|
|
|
};
|
|
|
|
|
|
|
|
@@ -292,7 +280,7 @@ DataConnection.prototype.handleSDP = function(message) {
|
|
|
self._makeAnswer();
|
|
|
}
|
|
|
}, function(err) {
|
|
|
- self._callback('Failed to setRemoteDescription');
|
|
|
+ self.emit('error', 'Failed to setRemoteDescription');
|
|
|
util.log('Failed to setRemoteDescription, ', err);
|
|
|
});
|
|
|
};
|
|
@@ -307,7 +295,7 @@ DataConnection.prototype.handleCandidate = function(message) {
|
|
|
|
|
|
DataConnection.prototype.handleLeave = function() {
|
|
|
util.log('Peer ' + this.peer + ' disconnected');
|
|
|
- this._cleanup();
|
|
|
+ this.close();
|
|
|
};
|
|
|
|
|
|
/*
|