Updated protocol-facing code to reflect new binary protocol

This is a squash of 6 small commits. Below is a summary of each. The gist
is that manager.js encoding and decoding portions were changed to work
with the new socket.io-protocol; this includes handling of encoding
a list of packets, and handling sequences of binary packets.

Commit 1 was the initial rewrite.

Commit 2 got all the tests passing via bug fixes.

Commit 3 updated the has-binary-data dependency and the build.

Commit 4 added nice comments.

Commits 5 and 6 updated build and engine.io dependencies respectively.
This commit is contained in:
Kevin Roark
2014-02-26 13:41:51 -05:00
parent b33afd6455
commit 67f15b16d2
4 changed files with 418 additions and 1783 deletions

View File

@@ -45,6 +45,7 @@ function Manager(socket, opts){
this.attempts = 0;
this.encoding = false;
this.packetBuffer = [];
this.reconstructor = null;
this.open();
}
@@ -213,7 +214,26 @@ Manager.prototype.onopen = function(){
*/
Manager.prototype.ondata = function(data){
this.emit('packet', parser.decode(data));
if ((global.Buffer && Buffer.isBuffer(data)) ||
(global.ArrayBuffer && data instanceof ArrayBuffer) ||
data.base64) { // this is binary data
if (!this.reconstructor) {
throw new Error('got binary data when not reconstructing a packet')
} else {
var packet = this.reconstructor.takeBinaryData(data);
if (packet) { // received final buffer
this.reconstructor = null;
this.emit('packet', packet);
}
}
} else { // not a binary object
var packet = parser.decode(data);
if (packet.type == parser.BINARY_EVENT) { // first part of 'buffer sequence'
this.reconstructor = new parser.BinaryReconstructor(packet);
} else { // this is a non-binary regular json packet
this.emit('packet', packet);
}
}
};
/**
@@ -272,8 +292,10 @@ Manager.prototype.packet = function(packet){
if (!self.encoding) { // encode, then write to engine with result
self.encoding = true;
parser.encode(packet, function(encodedPacket) {
self.engine.write(encodedPacket);
parser.encode(packet, function(encodedPackets) {
for (var i = 0; i < encodedPackets.length; i++) {
self.engine.write(encodedPackets[i]);
}
self.encoding = false;
self.processPacketQueue();
});

View File

@@ -9,7 +9,7 @@ var toArray = require('to-array');
var on = require('./on');
var bind = require('bind');
var debug = require('debug')('socket.io-client:socket');
var hasBin = require('has-binarydata');
var hasBin = require('has-binary-data');
var indexOf = require('indexof');
/**

View File

@@ -10,7 +10,7 @@
"client"
],
"dependencies": {
"engine.io-client": "LearnBoost/engine.io-client#b434277",
"engine.io-client": "LearnBoost/engine.io-client#7c9b0f",
"emitter": "http://github.com/component/emitter/archive/1.0.1.tar.gz",
"bind": "http://github.com/component/bind/archive/0.0.1.tar.gz",
"object-component": "0.0.3",
@@ -18,7 +18,7 @@
"parseuri": "0.0.2",
"to-array": "0.1.3",
"debug": "0.7.4",
"has-binarydata": "0.0.31",
"has-binary-data": "0.1.0",
"indexof": "0.0.1"
},
"devDependencies": {

File diff suppressed because it is too large Load Diff