Skip to content

Commit

Permalink
Adding support for true binary transmission, which is supported in So…
Browse files Browse the repository at this point in the history
…cket.IO version 1.0. See #16
  • Loading branch information
sffc committed Jul 27, 2014
1 parent 5a361d0 commit 585e704
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,26 @@ Defaults to `false`, which reads files as an octet array. This is necessary for

Set to `true` to read and transmit files as plain text instead. This will save bandwidth if you expect to transmit only text files. If you choose this option, it is recommended that you perform a filter by returning `false` to a `start` event if the file does not have a desired extension.

#### instance.useBuffer = false

Starting with Socket.IO 1.0, binary data may now be transmitted through the Web Socket. You may tell SIOFU to transmit files as binary data by setting this option to `true`. Defaults to `false`, which transmits files as base 64-encoded strings.

Advantages of enabling this option:

- Less overhead in the socket, since base 64 increases overhead by approximately 33%.
- No serialization and deserialization into and out of base 64 is required on the client and server side.

Disadvantages of enabling this option:

- Transmitting buffer types through a WebSocket is not supported in older browsers.
- This option is relatively new in both Socket.IO and Socket.IO File Upload and has not been rigorously tested.

As you use this option, [please leave feedback](https://github.com/vote539/socketio-file-upload/issues/16). I'm hoping to enable this feature by default in a future version of Socket.IO File Upload.

#### instance.serializeOctets = false

*This method is experimental, and has been deprecated in Socket.IO File Upload as of version 0.3 in favor of instance.useBuffer.*

Defaults to `false`, which transmits binary files as Base 64 data (with a 33% overhead).

Set to `true` to instead transmit the data as a serialized octet array. This will result in an overhead of over 1000% (not recommended for production applications).
Expand Down
8 changes: 6 additions & 2 deletions client.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
self.fileInputElementId = "siofu_input";
self.useText = false;
self.serializedOctets = false;
self.useBuffer = false;

/**
* Private method to dispatch a custom event on the instance.
Expand Down Expand Up @@ -117,7 +118,7 @@

// Private function to handle transmission of file data
var transmitPart = function(loaded){
var content;
var content, isBase64=false;
if(useText){
content = reader.result.slice(transmitPos, loaded);
}else{
Expand All @@ -129,7 +130,10 @@
// transmission in Base 64.
if(self.serializedOctets){
content = uintArr;
}else if(self.useBuffer){
content = uintArr.buffer;
}else{
isBase64 = true;
content = _uint8ArrayToBase64(uintArr);
}
}catch(error){
Expand All @@ -145,7 +149,7 @@
start: transmitPos,
end: loaded,
content: content,
base64: !self.serializedOctets
base64: isBase64
});
transmitPos = loaded;
};
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"socket.io"
],
"dependencies": {
"socket.io": "*"
"socket.io": "~1.0"
},
"files": [
"client.js",
Expand Down

0 comments on commit 585e704

Please sign in to comment.