mirror of
https://github.com/socketio/socket.io.git
synced 2026-04-30 03:00:39 -04:00
Merge pull request #2422 from nus-fboa2016-si/queryStringFix
Fix for Issue #331 on socket.io-client
This commit is contained in:
@@ -5,6 +5,7 @@
|
||||
|
||||
var parser = require('socket.io-parser');
|
||||
var debug = require('debug')('socket.io:client');
|
||||
var url = require('url');
|
||||
|
||||
/**
|
||||
* Module exports.
|
||||
@@ -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) {
|
||||
@@ -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;
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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(){
|
||||
|
||||
@@ -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;
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -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) + '',
|
||||
@@ -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()
|
||||
};
|
||||
};
|
||||
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user