Skip to content
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
2 changes: 2 additions & 0 deletions lib/namespace.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,8 @@ Namespace.prototype.initAdapter = function(){
*/

Namespace.prototype.use = function(fn){
debug('removing initial packet');
delete this.server.eio.initialPacket;
this.fns.push(fn);
return this;
};
Expand Down
7 changes: 4 additions & 3 deletions lib/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -297,9 +297,10 @@ Socket.prototype.onconnect = function(){
debug('socket connected - writing packet');
this.nsp.connected[this.id] = this;
this.join(this.id);
// the CONNECT packet for the default namespace
// has already been sent as an `initialPacket`
if (this.nsp.name !== '/') {
var skip = this.nsp.name === '/' && this.nsp.fns.length === 0;
if (skip) {
debug('packet already sent in initial handshake');
} else {
this.packet({ type: parser.CONNECT });
}
};
Expand Down
29 changes: 29 additions & 0 deletions test/socket.io.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ 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 @@ -2145,6 +2148,9 @@ 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 @@ -2163,6 +2169,9 @@ 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 All @@ -2186,6 +2195,26 @@ describe('socket.io', function(){
});
});

it('should only call connection after (lengthy) fns', function(done){
var srv = http();
var sio = io(srv);
var authenticated = false;

sio.use(function(socket, next){
setTimeout(function () {
authenticated = true;
next();
}, 300);
});
srv.listen(function(){
var socket = client(srv);
socket.on('connect', function(){
expect(authenticated).to.be(true);
done();
});
});
});

it('should be ignored if socket gets closed', function(done){
var srv = http();
var sio = io(srv);
Expand Down