Skip to content

Commit

Permalink
Clients now emit message event and inherit EventEmitter
Browse files Browse the repository at this point in the history
More encoding tests
Encoding fix for non-array arguments
Leveraged Array.isArray
Handling of undefined and null values
  • Loading branch information
rauchg committed Jul 28, 2010
1 parent 7b3781c commit 0edd16d
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
8 changes: 6 additions & 2 deletions lib/socket.io/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ var urlparse = require('url').parse,
frame = '\ufffdm\ufffd',

Client = module.exports = function(listener, req, res, options, head){
process.EventEmitter.call(this);
this.listener = listener;
this.options({
timeout: 12000,
Expand All @@ -15,6 +16,8 @@ Client = module.exports = function(listener, req, res, options, head){
this._onConnect(req, res);
};

require('sys').inherits(Client, process.EventEmitter);

Client.prototype.send = function(message){
if (!this.connected || !(this.connection.readyState === 'open' ||
this.connection.readyState === 'writeOnly')){
Expand All @@ -37,6 +40,7 @@ Client.prototype._onMessage = function(data){
if (messages[i].substr(0, 3) == '\ufffdh\ufffd'){
return this._onHeartbeat(data.substr(3));
}
this.emit('message', messages[i]);
this.listener._onClientMessage(messages[i], this);
}
};
Expand All @@ -51,9 +55,9 @@ Client.prototype._onConnect = function(req, res){

Client.prototype._encode = function(messages){
var ret = '', message,
messages = messages instanceof Array ? messages : [];
messages = Array.isArray(messages) ? messages : [messages];
for (var i = 0, l = messages.length; i < l; i++){
message = String(messages[i]);
message = messages[i] === null || messages[i] === undefined ? '' : String(messages[i]);
ret += frame + message.length + frame + message;
}
return ret;
Expand Down
2 changes: 1 addition & 1 deletion lib/socket.io/listener.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ var url = require('url'),
},

Listener = module.exports = function(server, options){
var self = this;
process.EventEmitter.call(this);
var self = this;
this.server = server;
this.options({
origins: '*:*',
Expand Down
8 changes: 5 additions & 3 deletions tests/client.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ module.exports = {
},

'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');
var client = new Client(listener, {}, {});
assert.equal(client._encode(['abcde', '123456789']), '\ufffdm\ufffd5\ufffdm\ufffdabcde' + '\ufffdm\ufffd9\ufffdm\ufffd123456789');
assert.equal(client._encode('asdasdsad'), '\ufffdm\ufffd9\ufffdm\ufffdasdasdsad');
assert.equal(client._encode(''), '\ufffdm\ufffd0\ufffdm\ufffd');
assert.equal(client._encode(null), '\ufffdm\ufffd0\ufffdm\ufffd');
}
};

0 comments on commit 0edd16d

Please sign in to comment.