Przeglądaj źródła

serialization format option

Michelle Bu 12 lat temu
rodzic
commit
5ea2b8c1cb
2 zmienionych plików z 36 dodań i 19 usunięć
  1. 35 19
      lib/connection.js
  2. 1 0
      lib/peer.js

+ 35 - 19
lib/connection.js

@@ -7,7 +7,8 @@ function DataConnection(id, peer, socket, options) {
 
   options = util.extend({
     config: { 'iceServers': [{ 'url': 'stun:stun.l.google.com:19302' }] },
-    reliable: false
+    reliable: false,
+    serialization: 'binary'
   }, options);
   this._options = options;
 
@@ -17,6 +18,7 @@ function DataConnection(id, peer, socket, options) {
   this.id = id;
   this.peer = peer;
   this.metadata = options.metadata;
+  this.serialization = options.serialization
 
   this._originator = (options.sdp === undefined);
   this._socket = socket;
@@ -182,6 +184,7 @@ DataConnection.prototype._makeOffer = function() {
         type: 'OFFER',
         payload: {
           sdp: offer,
+          serialization: self.serialization,
           metadata: self.metadata
         },
         dst: self.peer
@@ -233,19 +236,25 @@ DataConnection.prototype._cleanup = function() {
 // Handles a DataChannel message.
 DataConnection.prototype._handleDataMessage = function(e) {
   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. */
 DataConnection.prototype.send = function(data) {
   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 {
-    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);
+    }
   }
 };
 

+ 1 - 0
lib/peer.js

@@ -105,6 +105,7 @@ Peer.prototype._handleServerJSONMessage = function(message) {
     case 'OFFER':
       var options = {
         metadata: payload.metadata,
+        serialization: payload.serialization,
         sdp: payload.sdp,
         config: this._options.config,
       };