Skip to content

Commit

Permalink
[feature] Merge Engine.IO and Socket.IO handshake packets (#2833)
Browse files Browse the repository at this point in the history
  • Loading branch information
darrachequesne authored Feb 1, 2017
1 parent e1facd5 commit 54ff591
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 19 deletions.
27 changes: 18 additions & 9 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand Down
6 changes: 5 additions & 1 deletion lib/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
}
};

/**
Expand Down
9 changes: 0 additions & 9 deletions test/socket.io.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand Down

0 comments on commit 54ff591

Please sign in to comment.