Skip to content

Commit

Permalink
Fixed encoding / decoding
Browse files Browse the repository at this point in the history
Moved library files into lib/socket.io for future npm support
Added tests for encoding / decoding
Fixed inheritance of certain classes
Fixed options supports
  • Loading branch information
rauchg committed Jul 28, 2010
1 parent ce6fa86 commit fc40156
Show file tree
Hide file tree
Showing 12 changed files with 61 additions and 28 deletions.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
test:
./tests/support/expresso/bin/expresso -I lib $(TESTFLAGS) tests/*.test.js
2 changes: 1 addition & 1 deletion socket.io.js → index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
var Listener = require('./lib/listener');
var Listener = require('./lib/socket.io/listener');

this.listen = function(server, options){
return new Listener(server, options);
Expand Down
Binary file added lib/.DS_Store
Binary file not shown.
35 changes: 20 additions & 15 deletions lib/client.js → lib/socket.io/client.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var urlparse = require('url').parse,
options = require('./utils').options,
frame = '\ufffdm\ufffd',

Client = module.exports = function(listener, req, res, options, head){
Expand All @@ -14,8 +15,6 @@ Client = module.exports = function(listener, req, res, options, head){
this._onConnect(req, res);
};

sys.inherits(Client, require('./utils').options);

Client.prototype.send = function(message){
if (!this.connected || !(this.connection.readyState === 'open' ||
this.connection.readyState === 'writeOnly')){
Expand Down Expand Up @@ -55,25 +54,29 @@ Client.prototype._encode = function(messages){
messages = messages instanceof Array ? messages : [];
for (var i = 0, l = messages.length; i < l; i++){
message = String(messages[i]);
ret += message.length + frame + message;
ret += frame + message.length + frame + message;
}
return frame + ret;
return ret;
};

Client.prototype._decode = function(data){
if (data.substr(0, 3) !== frame) return false;
var messages = [];
var messages = [], number, n;
do {
for (var i = 0, n, number = '';; i++){
var n = data.substr(i, 1);
if (n === frame){
if (data.substr(0, 3) !== frame) return messages;
data = data.substr(3);
number = '', n = '';
for (var i = 0, l = data.length; i < l; i++){
n = Number(data.substr(i, 1));
if (data.substr(i, 1) == n){
number += n;
} else {
data = data.substr(number.length + frame.length)
number = Number(number);
break;
}
number += n;
}
}
messages.push(data.substr(i, i + number)); // here
data = data.substr(i + number);
messages.push(data.substr(0, number)); // here
data = data.substr(number);
} while(data !== '');
return messages;
};
Expand Down Expand Up @@ -103,7 +106,7 @@ Client.prototype._heartbeat = function(){
var self = this;
this.send('\ufffdh\ufffd' + ++this._heartbeats);
this._heartbitTimeout = setTimeout(function(){
self.close();
self._onClose();
}, this.listener.options.timeout);
};

Expand Down Expand Up @@ -149,4 +152,6 @@ Client.prototype._verifyOrigin = function(origin){
origins.indexOf(parts.host + ':' + parts.port) !== -1 ||
origins.indexOf(parts.host + ':*') !== -1 ||
origins.indexOf('*:' + parts.port) !== -1;
};
};

for (var i in options) Client.prototype[i] = options[i];
4 changes: 3 additions & 1 deletion lib/listener.js → lib/socket.io/listener.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
var url = require('url'),
sys = require('sys'),
options = require('./utils').options,
Client = require('./client'),
transports = {
'flashsocket': require('./transports/flashsocket'),
Expand Down Expand Up @@ -53,7 +55,7 @@ Listener = module.exports = function(server, options){
};

sys.inherits(Listener, process.EventEmitter);
sys.inherits(Listener, require('./utils').options);
for (var i in options) Client.prototype[i] = options[i];

Listener.prototype.broadcast = function(message, except){
for (var i = 0, l = this.clients.length; i < l; i++){
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
var Websocket = require('./websocket'),
net = require('net'),
var net = require('net'),
WebSocket = require('./websocket'),
listeners = [],

Flashsocket = module.exports = function(){};

sys.inherits(HTMLFile, Websocket);
require('sys').inherits(Flashsocket, WebSocket);

Flashsocket.httpUpgrade = true;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ var Client = require('../client'),

HTMLFile = module.exports = function(){};

sys.inherits(HTMLFile, Client);
require('sys').inherits(HTMLFile, Client);

HTMLFile.prototype._onConnect = function(req, res){
var self = this, body = '';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ var Client = require('../client'),
Buffer = require('buffer').Buffer,
crypto = require('crypto'),

WebSocket = exports.websocket = function(){};
WebSocket = module.exports = function(){};

sys.inherits(WebSocket, Client);
require('sys').inherits(WebSocket, Client);

WebSocket.prototype._onConnect = function(req, socket){
var self = this, headers = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ var Client = require('../client'),

Multipart = module.exports = function(){};

sys.inherits(HTMLFile, Client);
require('sys').inherits(Multipart, Client);

Multipart.prototype._onConnect = function(req, res){
var self = this, body = '', headers = {};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ var Client = require('../client'),

Polling = module.exports = function(){};

sys.inherits(Polling, Client);
require('sys').inherits(Polling, Client);

Polling.prototype._onConnect = function(req, res){
var self = this, body = '';
Expand Down
4 changes: 1 addition & 3 deletions lib/utils.js → lib/socket.io/utils.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
var Options = exports.options = function(){};

Options.prototype = {
exports.options = {
options: function(options, merge){
this.options = exports.merge(options || {}, merge || {});
}
Expand Down
26 changes: 26 additions & 0 deletions tests/client.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
var listener = require('socket.io/listener'),
Client = require('socket.io/client');

module.exports = {
'test decoding': function(assert){
var client = new Client(listener, {}, {}),
decoded = client._decode('\ufffdm\ufffd5\ufffdm\ufffdabcde' + '\ufffdm\ufffd9\ufffdm\ufffd123456789');
assert.equal(decoded.length, 2);
assert.equal(decoded[0], 'abcde');
assert.equal(decoded[1], '123456789');
},

'test decoding of bad framed messages': function(assert){
var client = new Client(listener, {}, {}),
decoded = client._decode('\ufffdm\ufffd5\ufffdm\ufffdabcde' + '\ufffdm\uffsdaasdfd9\ufffdm\ufffd1aaa23456789');
assert.equal(decoded.length, 1);
assert.equal(decoded[0], 'abcde');
assert.equal(decoded[1], undefined);
},

'test encoding': function(assert){
var client = new Client(listener, {}, {}),
encoded = client._encode(['abcde', '123456789']);
assert.equal(encoded, '\ufffdm\ufffd5\ufffdm\ufffdabcde' + '\ufffdm\ufffd9\ufffdm\ufffd123456789');
}
};

0 comments on commit fc40156

Please sign in to comment.