diff --git a/lib/socket.js b/lib/socket.js index a0e1d1320..ab6422694 100644 --- a/lib/socket.js +++ b/lib/socket.js @@ -332,8 +332,8 @@ Socket.prototype.onack = function (packet) { Socket.prototype.onconnect = function () { this.connected = true; this.disconnected = false; - this.emit('connect'); this.emitBuffered(); + this.emit('connect'); }; /** diff --git a/test/socket.js b/test/socket.js index 91abd9d81..691624ca5 100644 --- a/test/socket.js +++ b/test/socket.js @@ -176,4 +176,24 @@ describe('socket', function () { done(); }); }); + + it('should emit events in order', function (done) { + var socket = io('/', { autoConnect: false }); + var i = 0; + + socket.on('connect', function () { + socket.emit('echo', 'second', function () { + expect(++i).to.eql(2); + + socket.disconnect(); + done(); + }); + }); + + socket.emit('echo', 'first', function () { + expect(++i).to.eql(1); + }); + + socket.connect(); + }); }); diff --git a/test/support/server.js b/test/support/server.js index 23dd49f32..ca37cb643 100644 --- a/test/support/server.js +++ b/test/support/server.js @@ -40,6 +40,10 @@ server.on('connection', function (socket) { socket.emit('hi'); }); + socket.on('echo', function (arg, callback) { + callback(arg); + }); + // ack tests socket.on('ack', function () { socket.emit('ack', function (a, b) {