Skip to content

Commit

Permalink
fix: ignore packet received after disconnection
Browse files Browse the repository at this point in the history
Related: #3095

Backported from 494c64e
  • Loading branch information
darrachequesne committed Jun 26, 2022
1 parent dfded53 commit 22d4bdf
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -525,7 +525,11 @@ Socket.prototype.dispatch = function(event){
if (err) {
return self.error(err.data || err.message);
}
emit.apply(self, event);
if (self.connected) {
emit.apply(self, event);
} else {
debug("ignore packet received after disconnection");
}
});
}
this.run(event, dispatchSocket);
Expand Down
27 changes: 27 additions & 0 deletions test/socket.io.js
Original file line number Diff line number Diff line change
Expand Up @@ -1838,6 +1838,33 @@ describe('socket.io', function(){
});
});

it("should ignore a packet received after disconnection", (done) => {
const srv = http();
const sio = io(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();
});
});
});

it('should always trigger the callback (if provided) when joining a room', function(done){
var srv = http();
var sio = io(srv);
Expand Down

0 comments on commit 22d4bdf

Please sign in to comment.