Skip to content

Commit 2b21690

Browse files
[fix] Fix timing issues with middleware (#2948)
Using a middleware could previously lead to a connecting client receiving a connect event from the server before the server triggers its own connect event.
1 parent 832b8fc commit 2b21690

File tree

3 files changed

+35
-3
lines changed

3 files changed

+35
-3
lines changed

lib/namespace.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ Namespace.prototype.initAdapter = function(){
9999
*/
100100

101101
Namespace.prototype.use = function(fn){
102+
debug('removing initial packet');
103+
delete this.server.eio.initialPacket;
102104
this.fns.push(fn);
103105
return this;
104106
};

lib/socket.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -297,9 +297,10 @@ Socket.prototype.onconnect = function(){
297297
debug('socket connected - writing packet');
298298
this.nsp.connected[this.id] = this;
299299
this.join(this.id);
300-
// the CONNECT packet for the default namespace
301-
// has already been sent as an `initialPacket`
302-
if (this.nsp.name !== '/') {
300+
var skip = this.nsp.name === '/' && this.nsp.fns.length === 0;
301+
if (skip) {
302+
debug('packet already sent in initial handshake');
303+
} else {
303304
this.packet({ type: parser.CONNECT });
304305
}
305306
};

test/socket.io.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ describe('socket.io', function(){
8787
srv.set('authorization', function(o, f) { f(null, false); });
8888

8989
var socket = client(httpSrv);
90+
socket.on('connect', function(){
91+
expect().fail();
92+
});
9093
socket.on('error', function(err) {
9194
expect(err).to.be('Not authorized');
9295
done();
@@ -2145,6 +2148,9 @@ describe('socket.io', function(){
21452148
});
21462149
srv.listen(function(){
21472150
var socket = client(srv);
2151+
socket.on('connect', function(){
2152+
done(new Error('nope'));
2153+
});
21482154
socket.on('error', function(err){
21492155
expect(err).to.be('Authentication error');
21502156
done();
@@ -2163,6 +2169,9 @@ describe('socket.io', function(){
21632169
});
21642170
srv.listen(function(){
21652171
var socket = client(srv);
2172+
socket.on('connect', function(){
2173+
done(new Error('nope'));
2174+
});
21662175
socket.on('error', function(err){
21672176
expect(err).to.eql({ a: 'b', c: 3 });
21682177
done();
@@ -2186,6 +2195,26 @@ describe('socket.io', function(){
21862195
});
21872196
});
21882197

2198+
it('should only call connection after (lengthy) fns', function(done){
2199+
var srv = http();
2200+
var sio = io(srv);
2201+
var authenticated = false;
2202+
2203+
sio.use(function(socket, next){
2204+
setTimeout(function () {
2205+
authenticated = true;
2206+
next();
2207+
}, 300);
2208+
});
2209+
srv.listen(function(){
2210+
var socket = client(srv);
2211+
socket.on('connect', function(){
2212+
expect(authenticated).to.be(true);
2213+
done();
2214+
});
2215+
});
2216+
});
2217+
21892218
it('should be ignored if socket gets closed', function(done){
21902219
var srv = http();
21912220
var sio = io(srv);

0 commit comments

Comments
 (0)