diff --git a/lib/index.js b/lib/index.js index 871c436df0..340366e587 100644 --- a/lib/index.js +++ b/lib/index.js @@ -245,18 +245,27 @@ Server.prototype.attach = function(srv, opts){ // set origins verification opts.allowRequest = opts.allowRequest || this.checkRequest.bind(this); - // initialize engine - debug('creating engine.io instance with opts %j', opts); - this.eio = engine.attach(srv, opts); + var self = this; + + var connectPacket = { type: parser.CONNECT, nsp: '/' }; + this.encoder.encode(connectPacket, function (encodedPacket){ + // the CONNECT packet will be merged with Engine.IO handshake, + // to reduce the number of round trips + opts.initialPacket = encodedPacket; - // attach static file serving - if (this._serveClient) this.attachServe(srv); + // initialize engine + debug('creating engine.io instance with opts %j', opts); + self.eio = engine.attach(srv, opts); - // Export http server - this.httpServer = srv; + // attach static file serving + if (self._serveClient) self.attachServe(srv); - // bind to engine events - this.bind(this.eio); + // Export http server + self.httpServer = srv; + + // bind to engine events + self.bind(self.eio); + }); return this; }; diff --git a/lib/socket.js b/lib/socket.js index e97536f214..85911979ae 100644 --- a/lib/socket.js +++ b/lib/socket.js @@ -290,7 +290,11 @@ Socket.prototype.onconnect = function(){ debug('socket connected - writing packet'); this.nsp.connected[this.id] = this; this.join(this.id); - this.packet({ type: parser.CONNECT }); + // the CONNECT packet for the default namespace + // has already been sent as an `initialPacket` + if (this.nsp.name !== '/') { + this.packet({ type: parser.CONNECT }); + } }; /** diff --git a/test/socket.io.js b/test/socket.io.js index 9d69c19744..3d87e3dba5 100644 --- a/test/socket.io.js +++ b/test/socket.io.js @@ -87,9 +87,6 @@ describe('socket.io', function(){ srv.set('authorization', function(o, f) { f(null, false); }); var socket = client(httpSrv); - socket.on('connect', function(){ - expect().fail(); - }); socket.on('error', function(err) { expect(err).to.be('Not authorized'); done(); @@ -2131,9 +2128,6 @@ describe('socket.io', function(){ }); srv.listen(function(){ var socket = client(srv); - socket.on('connect', function(){ - done(new Error('nope')); - }); socket.on('error', function(err){ expect(err).to.be('Authentication error'); done(); @@ -2152,9 +2146,6 @@ describe('socket.io', function(){ }); srv.listen(function(){ var socket = client(srv); - socket.on('connect', function(){ - done(new Error('nope')); - }); socket.on('error', function(err){ expect(err).to.eql({ a: 'b', c: 3 }); done();