Skip to content

Commit

Permalink
fix: ignore packet received after disconnection
Browse files Browse the repository at this point in the history
Related: #3095
  • Loading branch information
darrachequesne committed Feb 25, 2021
1 parent 67a61e3 commit 494c64e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/socket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,11 @@ export class Socket extends EventEmitter {
if (err) {
return this._onerror(err);
}
super.emit.apply(this, event);
if (this.connected) {
super.emit.apply(this, event);
} else {
debug("ignore packet received after disconnection");
}
});
});
}
Expand Down
27 changes: 27 additions & 0 deletions test/socket.io.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1787,6 +1787,33 @@ describe("socket.io", () => {
});
});

it("should ignore a packet received after disconnection", (done) => {
const srv = createServer();
const sio = new Server(srv);

srv.listen(() => {
const clientSocket = client(srv);

const success = () => {
clientSocket.close();
sio.close();
done();
};

sio.on("connection", (socket) => {
socket.on("test", () => {
done(new Error("should not happen"));
});
socket.on("disconnect", success);
});

clientSocket.on("connect", () => {
clientSocket.emit("test", Buffer.alloc(10));
clientSocket.disconnect();
});
});
});

describe("onAny", () => {
it("should call listener", (done) => {
const srv = createServer();
Expand Down

0 comments on commit 494c64e

Please sign in to comment.