Skip to content

Instantly share code, notes, and snippets.

@nowelium
Created February 20, 2012 13:28
Show Gist options
  • Save nowelium/1869204 to your computer and use it in GitHub Desktop.
Save nowelium/1869204 to your computer and use it in GitHub Desktop.
todo ti-websocket-client payload size > 65536
//
// buffer size(65536) を超えてしまうと、上手くframe分割とかされないようなので
// なんとかしてみようとする試み(データは送れるけど、、、版)
//
WebSocket.prototype._create_frame = function(opcode, d, last_frame) {
if(typeof last_frame === 'undefined') {
last_frame = true;
}
if(last_frame === false && opcode >= 0x8 && opcode <= 0xf) {
return false;
}
var data = d || ''; //compress(d) // TODO
var length = Utils.byte_length(data);
var header_length = 2;
var mask_size = 6;
if(125 < length && length <= BUFFER_SIZE){
header_length += 2;
} else if(BUFFER_SIZE < length){
header_length += 8;
}
if(!this._masking_disabled){
header_length += 4;
}
// apply per frame compression
var out = Ti.createBuffer({ length: length + header_length + mask_size });
var outIndex = 0;
var byte1 = opcode;
if(last_frame) {
byte1 = byte1 | 0x80;
}
Ti.Codec.encodeNumber({
source: byte1,
dest: out,
position: outIndex++,
type: Ti.Codec.TYPE_BYTE,
});
if(length <= 125) {
var byte2 = length;
if(!this._masking_disabled) {
byte2 = (byte2 | 0x80); // # set masking bit
}
Ti.Codec.encodeNumber({
source: byte2,
dest: out,
position: outIndex++,
type: Ti.Codec.TYPE_BYTE
});
}
/*
else if(length < BUFFER_SIZE) { // # write 2 byte length
Ti.Codec.encodeNumber({
source: (126 | 0x80),
dest: out,
position: outIndex++,
type: Ti.Codec.TYPE_BYTE
});
Ti.Codec.encodeNumber({
source: length,
dest: out,
position: outIndex++,
type: Ti.Codec.TYPE_SHORT,
byteOrder: Ti.Codec.BIG_ENDIAN
});
outIndex += 2;
}
*/
else { // # write 8 byte length
Ti.Codec.encodeNumber({
source: (127 | 0x80),
dest: out,
position: outIndex++,
type: Ti.Codec.TYPE_BYTE
});
Ti.Codec.encodeNumber({
source: length,
dest: out,
position: outIndex,
type: Ti.Codec.TYPE_LONG,
byteOrder: Ti.Codec.BIG_ENDIAN
});
outIndex += 8;
}
//# mask data
outIndex = this._mask_payload(out, outIndex, data);
out.length = outIndex;
return out;
};
WebSocket.prototype._mask_payload = function(out, outIndex, payload) {
if(!this._masking_disabled) {
var i, masking_key = [];
for(i = 0; i < 4; ++i) {
var key = Math.floor(Math.random()*255) & 0xff;
masking_key.push(key);
Ti.Codec.encodeNumber({
source: key,
dest: out,
position: outIndex++,
type: Ti.Codec.TYPE_BYTE
});
}
var buffer = Ti.createBuffer({ length: BUFFER_SIZE });
var length = Ti.Codec.encodeString({
source: payload,
dest: buffer
});
buffer.length = length;
var string = Ti.Codec.decodeString({
source: buffer,
charset: Ti.Codec.CHARSET_ASCII
});
if(out.length < length){
out.length = string.length;
}
for(i = 0; i < string.length; ++i) {
Ti.Codec.encodeNumber({
source: string.charCodeAt(i) ^ masking_key[i % 4],
dest: out,
position: outIndex++,
type: Ti.Codec.TYPE_BYTE
});
}
return outIndex;
}
else {
var len = Ti.Codec.encodeString({
source: payload,
dest: out,
destPosition: outIndex
});
return len + outIndex;
}
};
WebSocket.prototype.send = function(data) {
if(data && this.readyState === OPEN) {
var frame = this._create_frame(0x01, data);
// var bytesWritten = this._socket.write(frame);
// return bytesWritten > 0;
var offset = 0;
var length = frame.length;
while(offset < length){
var clone = frame.clone(offset, length - offset);
var bytesWritten = this._socket.write(clone);
if(bytesWritten < 1){
return false;
}
offset += bytesWritten;
}
return true;
}
else {
return false;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment