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

Merge Engine.IO and Socket.IO handshake packets #2833

Merged
Show file tree
Hide file tree
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
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