Comparing version 0.3.4 to 0.3.5
@@ -72,3 +72,3 @@ /*! | ||
self.expect(2, function(data) { | ||
opcodes['1'].getData(readUInt16BE.call(data, 0)); | ||
opcodes['1'].getData(readUInt16BE.call(data, 0, true)); | ||
}); | ||
@@ -78,7 +78,7 @@ } | ||
self.expect(8, function(data) { | ||
if (readUInt32BE.call(data, 0) != 0) { | ||
if (readUInt32BE.call(data, 0, true) != 0) { | ||
self.error('packets with length spanning more than 32 bit is currently not supported', 1008); | ||
return; | ||
} | ||
opcodes['1'].getData(readUInt32BE.call(data, 4)); | ||
opcodes['1'].getData(readUInt32BE.call(data, 4, true)); | ||
}); | ||
@@ -127,3 +127,3 @@ } | ||
self.expect(2, function(data) { | ||
opcodes['2'].getData(readUInt16BE.call(data, 0)); | ||
opcodes['2'].getData(readUInt16BE.call(data, 0, true)); | ||
}); | ||
@@ -133,7 +133,7 @@ } | ||
self.expect(8, function(data) { | ||
if (readUInt32BE.call(data, 0) != 0) { | ||
if (readUInt32BE.call(data, 0, true) != 0) { | ||
self.error('packets with length spanning more than 32 bit is currently not supported', 1008); | ||
return; | ||
} | ||
opcodes['2'].getData(readUInt32BE.call(data, 4)); | ||
opcodes['2'].getData(readUInt32BE.call(data, 4, true)); | ||
}); | ||
@@ -320,3 +320,17 @@ } | ||
var toRead = Math.min(data.length, this.expectBuffer.length - this.expectOffset); | ||
data.copy(this.expectBuffer, this.expectOffset, 0, toRead); | ||
// although ugly, this is a much faster approach for small buffers, | ||
// than calling copy() | ||
var dest = this.expectBuffer; | ||
var offset = this.expectOffset; | ||
switch (toRead) { | ||
default: data.copy(dest, offset, 0, toRead); break; | ||
case 8: dest[offset+7] = data[7]; | ||
case 7: dest[offset+6] = data[6]; | ||
case 6: dest[offset+5] = data[5]; | ||
case 5: dest[offset+4] = data[4]; | ||
case 4: dest[offset+3] = data[3]; | ||
case 3: dest[offset+2] = data[2]; | ||
case 2: dest[offset+1] = data[1]; | ||
case 1: dest[offset] = data[0]; | ||
} | ||
this.expectOffset += toRead; | ||
@@ -344,5 +358,18 @@ if (toRead < data.length) { | ||
var prevOverflow = this.overflow; | ||
this.overflow = this.allocateFromPool(this.overflow.length + data.length); | ||
var dataLength = data.length; | ||
this.overflow = this.allocateFromPool(this.overflow.length + dataLength); | ||
prevOverflow.copy(this.overflow, 0); | ||
data.copy(this.overflow, prevOverflow.length); | ||
var dest = this.overflow; | ||
var offset = prevOverflow.length; | ||
switch (dataLength) { | ||
default: data.copy(dest, offset, 0, dataLength); break; | ||
case 8: dest[offset+7] = data[7]; | ||
case 7: dest[offset+6] = data[6]; | ||
case 6: dest[offset+5] = data[5]; | ||
case 5: dest[offset+4] = data[4]; | ||
case 4: dest[offset+3] = data[3]; | ||
case 3: dest[offset+2] = data[2]; | ||
case 2: dest[offset+1] = data[1]; | ||
case 1: dest[offset] = data[0]; | ||
} | ||
} | ||
@@ -502,2 +529,2 @@ } | ||
module.exports = Receiver; | ||
module.exports = Receiver; |
@@ -36,3 +36,5 @@ /*! | ||
function Sender (socket) { | ||
function Sender (socket, config) { | ||
this._sendCacheSize = (config && config.SendBufferCacheSize) ? config.SendBufferCacheSize : 65536; | ||
this._sendCache = new Buffer(this._sendCacheSize); | ||
this._socket = socket; | ||
@@ -61,3 +63,3 @@ this.firstFragment = true; | ||
var dataBuffer = new Buffer(2 + (data ? Buffer.byteLength(data) : 0)); | ||
writeUInt16BE.call(dataBuffer, code, 0); | ||
writeUInt16BE.call(dataBuffer, code, 0, true); | ||
if (dataBuffer.length > 2) dataBuffer.write(data, 2); | ||
@@ -136,3 +138,2 @@ var buf = this.frameData(0x8, dataBuffer, true, mask); | ||
*/ | ||
Sender.prototype.frameData = function(opcode, data, finalFragment, maskData) { | ||
@@ -155,3 +156,4 @@ if (!data) return new Buffer([opcode | (finalFragment ? 0x80 : 0), 0]); | ||
} | ||
var outputBuffer = new Buffer(dataLength + dataOffset); | ||
var outputBuffer = (this._sendCache && dataLength + dataOffset <= this._sendCacheSize) | ||
? this._sendCache : new Buffer(dataLength + dataOffset); | ||
if (finalFragment) opcode = opcode | 0x80; | ||
@@ -161,12 +163,18 @@ outputBuffer[0] = opcode; | ||
case 126: | ||
writeUInt16BE.call(outputBuffer, dataLength, 2); | ||
writeUInt16BE.call(outputBuffer, dataLength, 2, true); | ||
break; | ||
case 127: | ||
writeUInt32BE.call(outputBuffer, 0, 2); | ||
writeUInt32BE.call(outputBuffer, dataLength, 6); | ||
writeUInt32BE.call(outputBuffer, 0, 2, true); | ||
writeUInt32BE.call(outputBuffer, dataLength, 6, true); | ||
} | ||
if (maskData) { | ||
var mask = this._randomMask || (this._randomMask = getRandomMask()); | ||
mask.copy(outputBuffer, dataOffset - 4); | ||
bufferUtil.mask(data, mask, outputBuffer, dataOffset); | ||
//faster: | ||
outputBuffer[dataOffset - 4] = mask[0]; | ||
outputBuffer[dataOffset - 3] = mask[1]; | ||
outputBuffer[dataOffset - 2] = mask[2]; | ||
outputBuffer[dataOffset - 1] = mask[3]; | ||
//slower: | ||
//mask.copy(outputBuffer, dataOffset - 4); | ||
bufferUtil.mask(data, mask, outputBuffer, dataOffset, dataLength); | ||
secondByte = secondByte | 0x80; | ||
@@ -176,3 +184,3 @@ } | ||
outputBuffer[1] = secondByte; | ||
return outputBuffer; | ||
return outputBuffer.slice(0, dataOffset + dataLength); | ||
} | ||
@@ -179,0 +187,0 @@ |
@@ -309,2 +309,7 @@ /*! | ||
}); | ||
socket.on('close', function() { | ||
if (self._state == 'disconnected') return; | ||
self._state = 'disconnected'; | ||
self.emit('close', self._closeCode || 1000, self._closeMessage || ''); | ||
}); | ||
@@ -311,0 +316,0 @@ var receiver = new Receiver(); |
@@ -5,3 +5,3 @@ { | ||
"description": "simple and very fast websocket protocol client for node.js", | ||
"version": "0.3.4", | ||
"version": "0.3.5", | ||
"repository": { | ||
@@ -8,0 +8,0 @@ "type": "git", |
@@ -90,3 +90,4 @@ # ws: a node.js websocket implementation # | ||
Nothing worth bragging about at present. | ||
* Expose Sender and Receiver configuration options through WebSocket, and test that properly. | ||
* Cleanup configuration for Sender, and add similar bits to Receiver. | ||
@@ -93,0 +94,0 @@ ## License ## |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
122928
3138
117