diff --git a/lib/index.js b/lib/index.js index 6f128cebe..7be6dbf51 100644 --- a/lib/index.js +++ b/lib/index.js @@ -61,10 +61,27 @@ function lookup (uri, opts) { } io = cache[id]; } - - return io.socket(parsed.path); + if (parsed.query && !opts.query) { + opts.query = parsed.query; + } else if (opts && 'object' === typeof opts.query) { + opts.query = encodeQueryString(opts.query); + } + return io.socket(parsed.path, opts); +} +/** + * Helper method to parse query objects to string. + * @param {object} query + * @returns {string} + */ +function encodeQueryString (obj) { + var str = []; + for (var p in obj) { + if (obj.hasOwnProperty(p)) { + str.push(encodeURIComponent(p) + '=' + encodeURIComponent(obj[p])); + } + } + return str.join('&'); } - /** * Protocol version. * diff --git a/lib/manager.js b/lib/manager.js index 4efb31eac..1ad3ead32 100644 --- a/lib/manager.js +++ b/lib/manager.js @@ -205,7 +205,7 @@ Manager.prototype.maybeReconnectOnOpen = function () { */ Manager.prototype.open = -Manager.prototype.connect = function (fn) { +Manager.prototype.connect = function (fn, opts) { debug('readyState %s', this.readyState); if (~this.readyState.indexOf('open')) return this; @@ -350,10 +350,10 @@ Manager.prototype.onerror = function (err) { * @api public */ -Manager.prototype.socket = function (nsp) { +Manager.prototype.socket = function (nsp, opts) { var socket = this.nsps[nsp]; if (!socket) { - socket = new Socket(this, nsp); + socket = new Socket(this, nsp, opts); this.nsps[nsp] = socket; var self = this; socket.on('connecting', onConnecting); @@ -400,6 +400,7 @@ Manager.prototype.destroy = function (socket) { Manager.prototype.packet = function (packet) { debug('writing packet %j', packet); var self = this; + if (packet.query && packet.type === 0) packet.nsp += '?' + packet.query; if (!self.encoding) { // encode, then write to engine with result diff --git a/lib/socket.js b/lib/socket.js index dc676cee5..c7e4d91c4 100644 --- a/lib/socket.js +++ b/lib/socket.js @@ -52,7 +52,7 @@ var emit = Emitter.prototype.emit; * @api public */ -function Socket (io, nsp) { +function Socket (io, nsp, opts) { this.io = io; this.nsp = nsp; this.json = this; // compat @@ -62,6 +62,9 @@ function Socket (io, nsp) { this.sendBuffer = []; this.connected = false; this.disconnected = true; + if (opts && opts.query) { + this.query = opts.query; + } if (this.io.autoConnect) this.open(); } @@ -183,7 +186,11 @@ Socket.prototype.onopen = function () { // write connect packet if necessary if ('/' !== this.nsp) { - this.packet({ type: parser.CONNECT }); + if (this.query) { + this.packet({type: parser.CONNECT, query: this.query}); + } else { + this.packet({type: parser.CONNECT}); + } } }; diff --git a/test/socket.js b/test/socket.js index a5597204c..1fa0b6c59 100644 --- a/test/socket.js +++ b/test/socket.js @@ -95,4 +95,17 @@ describe('socket', function () { socket.compress(false).emit('hi'); }); }); + + it('should store query string as a property', function (done) { + var socket = io('/abc', {query: {a: 'b'}}); // passes in as a query obj + var socket2 = io('/abcd?b=c&d=e'); // passes in as a query string + var socket3 = io('/abc', {query: {'&a': '&=?a'}}); // checks that it encodes a string + expect(socket.query).to.be('a=b'); + expect(socket2.query).to.be('b=c&d=e'); + expect(socket3.query).to.be('%26a=%26%3D%3Fa'); + socket.disconnect(); + socket2.disconnect(); + socket3.disconnect(); + done(); + }); });