Skip to content

Commit 54ff591

Browse files
[feature] Merge Engine.IO and Socket.IO handshake packets (#2833)
1 parent e1facd5 commit 54ff591

File tree

3 files changed

+23
-19
lines changed

3 files changed

+23
-19
lines changed

lib/index.js

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -245,18 +245,27 @@ Server.prototype.attach = function(srv, opts){
245245
// set origins verification
246246
opts.allowRequest = opts.allowRequest || this.checkRequest.bind(this);
247247

248-
// initialize engine
249-
debug('creating engine.io instance with opts %j', opts);
250-
this.eio = engine.attach(srv, opts);
248+
var self = this;
249+
250+
var connectPacket = { type: parser.CONNECT, nsp: '/' };
251+
this.encoder.encode(connectPacket, function (encodedPacket){
252+
// the CONNECT packet will be merged with Engine.IO handshake,
253+
// to reduce the number of round trips
254+
opts.initialPacket = encodedPacket;
251255

252-
// attach static file serving
253-
if (this._serveClient) this.attachServe(srv);
256+
// initialize engine
257+
debug('creating engine.io instance with opts %j', opts);
258+
self.eio = engine.attach(srv, opts);
254259

255-
// Export http server
256-
this.httpServer = srv;
260+
// attach static file serving
261+
if (self._serveClient) self.attachServe(srv);
257262

258-
// bind to engine events
259-
this.bind(this.eio);
263+
// Export http server
264+
self.httpServer = srv;
265+
266+
// bind to engine events
267+
self.bind(self.eio);
268+
});
260269

261270
return this;
262271
};

lib/socket.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,11 @@ Socket.prototype.onconnect = function(){
290290
debug('socket connected - writing packet');
291291
this.nsp.connected[this.id] = this;
292292
this.join(this.id);
293-
this.packet({ type: parser.CONNECT });
293+
// the CONNECT packet for the default namespace
294+
// has already been sent as an `initialPacket`
295+
if (this.nsp.name !== '/') {
296+
this.packet({ type: parser.CONNECT });
297+
}
294298
};
295299

296300
/**

test/socket.io.js

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,6 @@ describe('socket.io', function(){
8787
srv.set('authorization', function(o, f) { f(null, false); });
8888

8989
var socket = client(httpSrv);
90-
socket.on('connect', function(){
91-
expect().fail();
92-
});
9390
socket.on('error', function(err) {
9491
expect(err).to.be('Not authorized');
9592
done();
@@ -2131,9 +2128,6 @@ describe('socket.io', function(){
21312128
});
21322129
srv.listen(function(){
21332130
var socket = client(srv);
2134-
socket.on('connect', function(){
2135-
done(new Error('nope'));
2136-
});
21372131
socket.on('error', function(err){
21382132
expect(err).to.be('Authentication error');
21392133
done();
@@ -2152,9 +2146,6 @@ describe('socket.io', function(){
21522146
});
21532147
srv.listen(function(){
21542148
var socket = client(srv);
2155-
socket.on('connect', function(){
2156-
done(new Error('nope'));
2157-
});
21582149
socket.on('error', function(err){
21592150
expect(err).to.eql({ a: 'b', c: 3 });
21602151
done();

0 commit comments

Comments
 (0)