diff --git a/lib/client.js b/lib/client.js index dd3c94b4b2..3c23ae155a 100644 --- a/lib/client.js +++ b/lib/client.js @@ -57,6 +57,10 @@ Client.prototype.setup = function(){ Client.prototype.connect = function(name){ debug('connecting to namespace %s', name); + if (!this.server.nsps[name]) { + this.packet({ type: parser.ERROR, nsp: name, data : 'Invalid namespace'}); + return; + } var nsp = this.server.of(name); if ('/' != name && !this.nsps['/']) { this.connectBuffer.push(name); diff --git a/test/socket.io.js b/test/socket.io.js index b5c23c924c..8cc7f45b5d 100644 --- a/test/socket.io.js +++ b/test/socket.io.js @@ -377,6 +377,8 @@ describe('socket.io', function(){ var srv = http(); var sio = io(srv); srv.listen(function(){ + sio.of('/chat'); + sio.of('/news'); var chat = client(srv, '/chat'); var news = client(srv, '/news'); var total = 2; @@ -491,6 +493,18 @@ describe('socket.io', function(){ }); }); }); + + it('should return error connecting to non-existent namespace', function(done){ + var srv = http(); + var sio = io(srv); + srv.listen(function(){ + var socket = client(srv,'/doesnotexist'); + socket.on('error', function(err) { + expect(err).to.be('Invalid namespace'); + done(); + }); + }); + }); }); describe('socket', function(){