|
@@ -2,11 +2,11 @@
|
|
* Wraps a DataChannel between two Peers.
|
|
* Wraps a DataChannel between two Peers.
|
|
*/
|
|
*/
|
|
function DataConnection(peer, provider, options) {
|
|
function DataConnection(peer, provider, options) {
|
|
- if (!(this instanceof DataConnection)) return new DataConnection(peer, options);
|
|
|
|
|
|
+ if (!(this instanceof DataConnection)) return new DataConnection(peer, provider, options);
|
|
EventEmitter.call(this);
|
|
EventEmitter.call(this);
|
|
|
|
|
|
// TODO: perhaps default serialization should be binary-utf8?
|
|
// TODO: perhaps default serialization should be binary-utf8?
|
|
- options = util.extend({
|
|
|
|
|
|
+ this.options = util.extend({
|
|
serialization: 'binary'
|
|
serialization: 'binary'
|
|
}, options);
|
|
}, options);
|
|
|
|
|
|
@@ -16,30 +16,19 @@ function DataConnection(peer, provider, options) {
|
|
this.peer = peer;
|
|
this.peer = peer;
|
|
this.provider = provider;
|
|
this.provider = provider;
|
|
|
|
|
|
- if (options) {
|
|
|
|
- this.label = options.label;
|
|
|
|
- this.metadata = options.metadata;
|
|
|
|
- this.serialization = options.serialization;
|
|
|
|
- this.reliable = options.reliable;
|
|
|
|
|
|
+ this.label = this.options.label;
|
|
|
|
+ this.metadata = this.options.metadata; // TODO: metadata could also be a part of the paylod.
|
|
|
|
+ this.serialization = this.options.serialization;
|
|
|
|
+ this.reliable = this.options.reliable;
|
|
|
|
|
|
- this.id = options._id;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- if (!this.id) {
|
|
|
|
- this.id = DataConnection._idPrefix + util.randomToken();
|
|
|
|
- }
|
|
|
|
|
|
+ this.id = this.options._id || DataConnection._idPrefix + util.randomToken();
|
|
|
|
|
|
this._pc = Negotiator.startConnection(
|
|
this._pc = Negotiator.startConnection(
|
|
this.peer,
|
|
this.peer,
|
|
this.id,
|
|
this.id,
|
|
this.provider,
|
|
this.provider,
|
|
- (options && options.payload) ? options.payload : null
|
|
|
|
|
|
+ this.options._payload
|
|
);
|
|
);
|
|
-
|
|
|
|
- /*this._dc = dc;
|
|
|
|
- if (this._dc) {
|
|
|
|
- this._configureDataChannel();
|
|
|
|
- }*/
|
|
|
|
}
|
|
}
|
|
|
|
|
|
util.inherits(DataConnection, EventEmitter);
|
|
util.inherits(DataConnection, EventEmitter);
|
|
@@ -49,6 +38,7 @@ DataConnection._idPrefix = 'dc_';
|
|
/** Called by the Negotiator when the DataChannel is ready. */
|
|
/** Called by the Negotiator when the DataChannel is ready. */
|
|
DataConnection.prototype.initialize = function(dc) {
|
|
DataConnection.prototype.initialize = function(dc) {
|
|
this._dc = dc;
|
|
this._dc = dc;
|
|
|
|
+ this._configureDataChannel();
|
|
}
|
|
}
|
|
|
|
|
|
DataConnection.prototype._configureDataChannel = function() {
|
|
DataConnection.prototype._configureDataChannel = function() {
|
|
@@ -92,6 +82,7 @@ DataConnection.prototype._cleanup = function() {
|
|
this._dc = null;
|
|
this._dc = null;
|
|
}
|
|
}
|
|
this.open = false;
|
|
this.open = false;
|
|
|
|
+ // Negotiator will listen for this and take care of the PC if appropriate.
|
|
this.emit('close');
|
|
this.emit('close');
|
|
};
|
|
};
|
|
|
|
|
|
@@ -121,12 +112,6 @@ DataConnection.prototype._handleDataMessage = function(e) {
|
|
this.emit('data', data);
|
|
this.emit('data', data);
|
|
}
|
|
}
|
|
|
|
|
|
-DataConnection.prototype.addDC = function(dc) {
|
|
|
|
- this._dc = dc;
|
|
|
|
- this._configureDataChannel();
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-
|
|
|
|
/**
|
|
/**
|
|
* Exposed functionality for users.
|
|
* Exposed functionality for users.
|
|
*/
|
|
*/
|
|
@@ -142,7 +127,7 @@ DataConnection.prototype.close = function() {
|
|
/** Allows user to send data. */
|
|
/** Allows user to send data. */
|
|
DataConnection.prototype.send = function(data) {
|
|
DataConnection.prototype.send = function(data) {
|
|
if (!this.open) {
|
|
if (!this.open) {
|
|
- this.emit('error', new Error('Connection no longer open.'));
|
|
|
|
|
|
+ this.emit('error', new Error('Connection is not open. You should listen for the `open` event before sending messages.'));
|
|
}
|
|
}
|
|
if (this._reliable) {
|
|
if (this._reliable) {
|
|
// Note: reliable shim sending will make it so that you cannot customize
|
|
// Note: reliable shim sending will make it so that you cannot customize
|