Skip to content

Commit

Permalink
fix: properly parse the CONNECT packet in v2 compatibility mode
Browse files Browse the repository at this point in the history
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
  • Loading branch information
darrachequesne committed Feb 3, 2021
1 parent 4f2e9a7 commit 6f4bd7f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Decoder, Encoder, Packet, PacketType } from "socket.io-parser";
import debugModule = require("debug");
import url = require("url");
import type { IncomingMessage } from "http";
import type { Namespace, Server } from "./index";
import type { Socket } from "./socket";
Expand Down Expand Up @@ -222,7 +223,12 @@ export class Client {
*/
private ondecoded(packet: Packet): void {
if (PacketType.CONNECT === packet.type) {
this.connect(packet.nsp, packet.data);
if (this.conn.protocol === 3) {
const parsed = url.parse(packet.nsp, true);
this.connect(parsed.pathname!, parsed.query);
} else {
this.connect(packet.nsp, packet.data);
}
} else {
const socket = this.nsps.get(packet.nsp);
if (socket) {
Expand Down
26 changes: 26 additions & 0 deletions test/socket.io.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2479,6 +2479,32 @@ describe("socket.io", () => {
});
});

it("should be able to connect to a namespace with a query", (done) => {
const srv = createServer();
const sio = new Server(srv, {
allowEIO3: true,
});

srv.listen(async () => {
const port = (srv.address() as AddressInfo).port;
const clientSocket = io_v2.connect(
`http://localhost:${port}/the-namespace`,
{
multiplex: false,
}
);
clientSocket.query = { test: "123" };

const [socket]: Array<any> = await Promise.all([
waitFor(sio.of("/the-namespace"), "connection"),
waitFor(clientSocket, "connect"),
]);

expect(socket.handshake.auth).to.eql({ test: "123" });
success(sio, clientSocket, done);
});
});

it("should not connect if `allowEIO3` is false (default)", (done) => {
const srv = createServer();
const sio = new Server(srv);
Expand Down

0 comments on commit 6f4bd7f

Please sign in to comment.