Skip to content

Commit 6f4bd7f

Browse files
fix: properly parse the CONNECT packet in v2 compatibility mode
In Socket.IO v2, the Socket query option was appended to the namespace in the CONNECT packet: { type: 0, nsp: "/my-namespace?abc=123" } Note: the "query" option on the client-side (v2) will be found in the "auth" attribute on the server-side: ``` // client-side const socket = io("/nsp1", { query: { abc: 123 } }); socket.query = { abc: 456 }; // server-side const io = require("socket.io")(httpServer, { allowEIO3: true // enable compatibility mode }); io.of("/nsp1").on("connection", (socket) => { console.log(socket.handshake.auth); // { abc: 456 } (the Socket query) console.log(socket.handshake.query.abc); // 123 (the Manager query) }); More information here: 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 Related: #3791
1 parent 4f2e9a7 commit 6f4bd7f

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

lib/client.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Decoder, Encoder, Packet, PacketType } from "socket.io-parser";
22
import debugModule = require("debug");
3+
import url = require("url");
34
import type { IncomingMessage } from "http";
45
import type { Namespace, Server } from "./index";
56
import type { Socket } from "./socket";
@@ -222,7 +223,12 @@ export class Client {
222223
*/
223224
private ondecoded(packet: Packet): void {
224225
if (PacketType.CONNECT === packet.type) {
225-
this.connect(packet.nsp, packet.data);
226+
if (this.conn.protocol === 3) {
227+
const parsed = url.parse(packet.nsp, true);
228+
this.connect(parsed.pathname!, parsed.query);
229+
} else {
230+
this.connect(packet.nsp, packet.data);
231+
}
226232
} else {
227233
const socket = this.nsps.get(packet.nsp);
228234
if (socket) {

test/socket.io.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2479,6 +2479,32 @@ describe("socket.io", () => {
24792479
});
24802480
});
24812481

2482+
it("should be able to connect to a namespace with a query", (done) => {
2483+
const srv = createServer();
2484+
const sio = new Server(srv, {
2485+
allowEIO3: true,
2486+
});
2487+
2488+
srv.listen(async () => {
2489+
const port = (srv.address() as AddressInfo).port;
2490+
const clientSocket = io_v2.connect(
2491+
`http://localhost:${port}/the-namespace`,
2492+
{
2493+
multiplex: false,
2494+
}
2495+
);
2496+
clientSocket.query = { test: "123" };
2497+
2498+
const [socket]: Array<any> = await Promise.all([
2499+
waitFor(sio.of("/the-namespace"), "connection"),
2500+
waitFor(clientSocket, "connect"),
2501+
]);
2502+
2503+
expect(socket.handshake.auth).to.eql({ test: "123" });
2504+
success(sio, clientSocket, done);
2505+
});
2506+
});
2507+
24822508
it("should not connect if `allowEIO3` is false (default)", (done) => {
24832509
const srv = createServer();
24842510
const sio = new Server(srv);

0 commit comments

Comments
 (0)