Skip to content

Commit

Permalink
Merge pull request #2422 from nus-fboa2016-si/queryStringFix
Browse files Browse the repository at this point in the history
Fix for Issue #331 on socket.io-client
  • Loading branch information
rauchg committed Apr 11, 2016
2 parents 438ad63 + f9db729 commit fb0253e
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 10 deletions.
7 changes: 4 additions & 3 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

var parser = require('socket.io-parser');
var debug = require('debug')('socket.io:client');
var url = require('url');

/**
* Module exports.
Expand Down Expand Up @@ -58,7 +59,7 @@ Client.prototype.setup = function(){
* @api private
*/

Client.prototype.connect = function(name){
Client.prototype.connect = function(name, query){
debug('connecting to namespace %s', name);
var nsp = this.server.nsps[name];
if (!nsp) {
Expand All @@ -72,7 +73,7 @@ Client.prototype.connect = function(name){
}

var self = this;
var socket = nsp.add(this, function(){
var socket = nsp.add(this, query, function(){
self.sockets[socket.id] = socket;
self.nsps[nsp.name] = socket;

Expand Down Expand Up @@ -186,7 +187,7 @@ Client.prototype.ondata = function(data){

Client.prototype.ondecoded = function(packet) {
if (parser.CONNECT == packet.type) {
this.connect(packet.nsp);
this.connect(url.parse(packet.nsp).pathname, url.parse(packet.nsp, true).query);
} else {
var socket = this.nsps[packet.nsp];
if (socket) {
Expand Down
4 changes: 2 additions & 2 deletions lib/namespace.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,9 @@ Namespace.prototype['in'] = function(name){
* @api private
*/

Namespace.prototype.add = function(client, fn){
Namespace.prototype.add = function(client, query, fn){
debug('adding socket to nsp %s', this.name);
var socket = new Socket(this, client);
var socket = new Socket(this, client, query);
var self = this;
this.run(socket, function(err){
process.nextTick(function(){
Expand Down
20 changes: 16 additions & 4 deletions lib/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ var emit = Emitter.prototype.emit;
* @api public
*/

function Socket(nsp, client){
function Socket(nsp, client, query){
this.nsp = nsp;
this.server = nsp.server;
this.adapter = this.nsp.adapter;
Expand All @@ -66,7 +66,7 @@ function Socket(nsp, client){
this.acks = {};
this.connected = true;
this.disconnected = false;
this.handshake = this.buildHandshake();
this.handshake = this.buildHandshake(query);
}

/**
Expand Down Expand Up @@ -103,7 +103,19 @@ Socket.prototype.__defineGetter__('request', function(){
* @api private
*/

Socket.prototype.buildHandshake = function(){
Socket.prototype.buildHandshake = function(query){
var self = this;
function buildQuery(){
var requestQuery = url.parse(self.request.url, true).query;
//if socket-specific query exist, replace query strings in requestQuery
if(query){
query.t = requestQuery.t;
query.EIO = requestQuery.EIO;
query.transport = requestQuery.transport;
return query;
}
return requestQuery || {};
}
return {
headers: this.request.headers,
time: (new Date) + '',
Expand All @@ -112,7 +124,7 @@ Socket.prototype.buildHandshake = function(){
secure: !!this.request.connection.encrypted,
issued: +(new Date),
url: this.request.url,
query: url.parse(this.request.url, true).query || {}
query: buildQuery()
};
};

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"dependencies": {
"engine.io": "1.6.8",
"socket.io-parser": "2.2.6",
"socket.io-client": "1.4.5",
"socket.io-client": "git://github.com/nus-fboa2016-si/socket.io-client#e0580ef4",
"socket.io-adapter": "0.4.0",
"has-binary": "0.1.7",
"debug": "2.2.0"
Expand Down
18 changes: 18 additions & 0 deletions test/socket.io.js
Original file line number Diff line number Diff line change
Expand Up @@ -1533,6 +1533,24 @@ describe('socket.io', function(){
});
});
});

it('should see query parameters sent from secondary namespace connections in handshake object', function(done){
var srv = http();
var sio = io(srv);
var addr = srv.listen().address();
var url = 'ws://localhost:' + addr.port;
var client1 = ioc(url);
var client2 = ioc(url + '/connection2', {query: {key1: 'aa', key2: '&=bb'}});
sio.on('connection', function(s){
});
sio.of('/connection2').on('connection', function(s){
expect(s.handshake.query.key1).to.be('aa');
expect(s.handshake.query.key2).to.be('&=bb');
done();
});


});

it('should handle very large json', function(done){
this.timeout(30000);
Expand Down

0 comments on commit fb0253e

Please sign in to comment.