diff --git a/lib/socket.js b/lib/socket.js index 33ad8ba4a6..dacaa440ca 100644 --- a/lib/socket.js +++ b/lib/socket.js @@ -66,8 +66,8 @@ function Socket(nsp, client, query){ this.conn = client.conn; this.rooms = {}; this.acks = {}; - this.connected = true; - this.disconnected = false; + this.connected = false; + this.disconnected = true; this.handshake = this.buildHandshake(query); this.fns = []; this.flags = {}; @@ -300,6 +300,8 @@ Socket.prototype.leaveAll = function(){ Socket.prototype.onconnect = function(){ debug('socket connected - writing packet'); + this.connected = true; + this.disconnected = false; this.nsp.connected[this.id] = this; this.join(this.id); var skip = this.nsp.name === '/' && this.nsp.fns.length === 0; diff --git a/test/socket.io.js b/test/socket.io.js index 53ca2e656a..aa786495a0 100644 --- a/test/socket.io.js +++ b/test/socket.io.js @@ -2442,6 +2442,25 @@ describe('socket.io', function(){ if (++count === 2) done(); }); }); + + it("should only set `connected` to true after the middleware execution", (done) => { + const httpServer = http(); + const sio = io(httpServer); + + const clientSocket = client(httpServer, "/"); + + sio.use((socket, next) => { + expect(socket.connected).to.be(false); + expect(socket.disconnected).to.be(true); + next(); + }); + + sio.on("connection", (socket) => { + expect(socket.connected).to.be(true); + expect(socket.disconnected).to.be(false); + done(); + }); + }); }); describe('socket middleware', function(done){