Skip to content

Commit

Permalink
Merge Engine.IO and Socket.IO handshake packets
Browse files Browse the repository at this point in the history
  • Loading branch information
darrachequesne committed Jan 24, 2017
1 parent 3b92cc2 commit 1491153
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 19 deletions.
26 changes: 17 additions & 9 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,18 +245,26 @@ 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, for faster connection
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 1491153

Please sign in to comment.