Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix middleware initialization #2969

Merged
merged 1 commit into from
Jun 12, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 26 additions & 12 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -245,29 +245,43 @@ Server.prototype.attach = function(srv, opts){
// set origins verification
opts.allowRequest = opts.allowRequest || this.checkRequest.bind(this);

var self = this;
if (this.sockets.fns.length > 0) {
this.initEngine(srv, opts);
return this;
}

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;

// initialize engine
debug('creating engine.io instance with opts %j', opts);
self.eio = engine.attach(srv, opts);
self.initEngine(srv, opts);
});
return this;
};

/**
* Initialize engine
*
* @param {Object} options passed to engine.io
* @api private
*/

// attach static file serving
if (self._serveClient) self.attachServe(srv);
Server.prototype.initEngine = function(srv, opts){
// initialize engine
debug('creating engine.io instance with opts %j', opts);
this.eio = engine.attach(srv, opts);

// Export http server
self.httpServer = srv;
// attach static file serving
if (this._serveClient) this.attachServe(srv);

// bind to engine events
self.bind(self.eio);
});
// Export http server
this.httpServer = srv;

return this;
// bind to engine events
this.bind(this.eio);
};

/**
6 changes: 4 additions & 2 deletions lib/namespace.js
Original file line number Diff line number Diff line change
@@ -99,8 +99,10 @@ Namespace.prototype.initAdapter = function(){
*/

Namespace.prototype.use = function(fn){
debug('removing initial packet');
delete this.server.eio.initialPacket;
if (this.server.eio) {
debug('removing initial packet');
delete this.server.eio.initialPacket;
}
this.fns.push(fn);
return this;
};
13 changes: 13 additions & 0 deletions test/socket.io.js
Original file line number Diff line number Diff line change
@@ -2268,6 +2268,19 @@ describe('socket.io', function(){
});
});
});

it('should disable the merge of handshake packets', function(done){
var srv = http();
var sio = io();
sio.use(function(socket, next){
next();
});
sio.listen(srv);
var socket = client(srv);
socket.on('connect', function(){
done();
});
});
});

describe('socket middleware', function(done){