-
Notifications
You must be signed in to change notification settings - Fork 10.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Incorrect query object when connection to different namespace #3495
Comments
I have found the cause of the bug, it is located in socket.js: 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
// The order of query & requestQuery should be swapped! The general
// `requestQuery` currently overrides socket-specific `query`, but this
// should be the other way around.
// return Object.assign({}, query, requestQuery);
// It should be:
return Object.assign({}, requestQuery, query);
}
return {
// ...
query: buildQuery()
};
}; I'm going to file a PR for this. |
For people having this issue as well, until #3496 is accepted and published on npm, you can fix it by including the following in your server code: const url = require('url');
const Socket = require('socket.io/lib/socket');
Socket.prototype.buildHandshake = function(query) {
let self = this;
function buildQuery() {
let requestQuery = url.parse(self.request.url, true).query;
return Object.assign({}, requestQuery, query);
}
return {
headers: this.request.headers,
time: (new Date) + '',
address: this.conn.remoteAddress,
xdomain: !!this.request.headers.origin,
secure: !!this.request.connection.encrypted,
issued: +(new Date),
url: this.request.url,
query: buildQuery()
};
}; I can confirm that this fixed my issue which led me to discover this bug. |
The `query` option of the Manager had the priority over the one of the Socket instance, which meant updating the Socket#query object on the client-side was not reflected in the Socket#handshake object on the server-side. Please note that the behavior of the `query` option is still a bit weird in Socket.IO v2, as it only applies to non-default namespace. This is fixed in v3: - https://socket.io/docs/v3/migrating-from-2-x-to-3-0/#Add-a-clear-distinction-between-the-Manager-query-option-and-the-Socket-query-option - https://socket.io/docs/v3/middlewares/#Sending-credentials Fixes #3495
Fixed by d33a619 (included in Please note that this does not apply to Socket.IO v3, since the |
Current behaviour
What is actually happening?
When one connects to a different namespace with multiplexing enabled such as
the updated query is not parsed correctly on the server. The server side code
logs
When multiplexing is explicitly disabled by setting
the server logs
as expected.
Steps to reproduce
Fork https://github.com/sebamarynissen/socket.io-fiddle. Run
npm start
, open up a browser and go tohttp://localhost:3000
.Expected behaviour
The query object should be the same, regardless of whether multiplexing is enabled or not.
Setup
Other information
When I open up the WS network tab in Chrome devtools, it logs that
is sent to the server, so it seems that at least the client sends the correct query to the server. Hence I guess that this is a server related issue.
The text was updated successfully, but these errors were encountered: