|
@@ -7,7 +7,8 @@ function DataConnection(id, peer, socket, options) {
|
|
|
|
|
|
options = util.extend({
|
|
options = util.extend({
|
|
config: { 'iceServers': [{ 'url': 'stun:stun.l.google.com:19302' }] },
|
|
config: { 'iceServers': [{ 'url': 'stun:stun.l.google.com:19302' }] },
|
|
- reliable: false
|
|
|
|
|
|
+ reliable: false,
|
|
|
|
+ serialization: 'binary'
|
|
}, options);
|
|
}, options);
|
|
this._options = options;
|
|
this._options = options;
|
|
|
|
|
|
@@ -17,6 +18,7 @@ function DataConnection(id, peer, socket, options) {
|
|
this.id = id;
|
|
this.id = id;
|
|
this.peer = peer;
|
|
this.peer = peer;
|
|
this.metadata = options.metadata;
|
|
this.metadata = options.metadata;
|
|
|
|
+ this.serialization = options.serialization
|
|
|
|
|
|
this._originator = (options.sdp === undefined);
|
|
this._originator = (options.sdp === undefined);
|
|
this._socket = socket;
|
|
this._socket = socket;
|
|
@@ -182,6 +184,7 @@ DataConnection.prototype._makeOffer = function() {
|
|
type: 'OFFER',
|
|
type: 'OFFER',
|
|
payload: {
|
|
payload: {
|
|
sdp: offer,
|
|
sdp: offer,
|
|
|
|
+ serialization: self.serialization,
|
|
metadata: self.metadata
|
|
metadata: self.metadata
|
|
},
|
|
},
|
|
dst: self.peer
|
|
dst: self.peer
|
|
@@ -233,19 +236,25 @@ DataConnection.prototype._cleanup = function() {
|
|
// Handles a DataChannel message.
|
|
// Handles a DataChannel message.
|
|
DataConnection.prototype._handleDataMessage = function(e) {
|
|
DataConnection.prototype._handleDataMessage = function(e) {
|
|
var self = this;
|
|
var self = this;
|
|
- if (e.data.constructor === Blob) {
|
|
|
|
- util.blobToArrayBuffer(e.data, function(ab) {
|
|
|
|
- var data = BinaryPack.unpack(ab);
|
|
|
|
- self.emit('data', data);
|
|
|
|
- });
|
|
|
|
- } else if (e.data.constructor === ArrayBuffer) {
|
|
|
|
- var data = BinaryPack.unpack(e.data);
|
|
|
|
- self.emit('data', data);
|
|
|
|
- } else if (e.data.constructor === String) {
|
|
|
|
- var ab = util.binaryStringToArrayBuffer(e.data);
|
|
|
|
- var data = BinaryPack.unpack(ab);
|
|
|
|
- self.emit('data', data);
|
|
|
|
|
|
+ var data = e.data;
|
|
|
|
+ var datatype = data.constructor;
|
|
|
|
+ if (this.serialization === 'binary') {
|
|
|
|
+ if (datatype === Blob) {
|
|
|
|
+ util.blobToArrayBuffer(data, function(ab) {
|
|
|
|
+ data = BinaryPack.unpack(ab);
|
|
|
|
+ self.emit('data', data);
|
|
|
|
+ });
|
|
|
|
+ return;
|
|
|
|
+ } else if (datatype === ArrayBuffer) {
|
|
|
|
+ data = BinaryPack.unpack(data);
|
|
|
|
+ } else if (datatype === String) {
|
|
|
|
+ var ab = util.binaryStringToArrayBuffer(data);
|
|
|
|
+ data = BinaryPack.unpack(ab);
|
|
|
|
+ }
|
|
|
|
+ } else if (this.serialization === 'json') {
|
|
|
|
+ data = JSON.parse(data);
|
|
}
|
|
}
|
|
|
|
+ this.emit('data', data);
|
|
};
|
|
};
|
|
|
|
|
|
|
|
|
|
@@ -271,13 +280,20 @@ DataConnection.prototype.close = function() {
|
|
/** Allows user to send data. */
|
|
/** Allows user to send data. */
|
|
DataConnection.prototype.send = function(data) {
|
|
DataConnection.prototype.send = function(data) {
|
|
var self = this;
|
|
var self = this;
|
|
- var blob = BinaryPack.pack(data);
|
|
|
|
- if (util.browserisms === 'Webkit') {
|
|
|
|
- util.blobToBinaryString(blob, function(str){
|
|
|
|
- self._dc.send(str);
|
|
|
|
- });
|
|
|
|
|
|
+ if (this.serialization === 'none') {
|
|
|
|
+ this._dc.send(data);
|
|
|
|
+ } else if (this.serialization === 'json') {
|
|
|
|
+ this._dc.send(JSON.stringify(data));
|
|
} else {
|
|
} else {
|
|
- this._dc.send(blob);
|
|
|
|
|
|
+ var blob = BinaryPack.pack(data);
|
|
|
|
+ // DataChannel currently only supports strings.
|
|
|
|
+ if (util.browserisms === 'Webkit') {
|
|
|
|
+ util.blobToBinaryString(blob, function(str){
|
|
|
|
+ self._dc.send(str);
|
|
|
|
+ });
|
|
|
|
+ } else {
|
|
|
|
+ this._dc.send(blob);
|
|
|
|
+ }
|
|
}
|
|
}
|
|
};
|
|
};
|
|
|
|
|