diff --git a/lib/namespace.js b/lib/namespace.js index db7db2cca2..d2269fa259 100644 --- a/lib/namespace.js +++ b/lib/namespace.js @@ -49,7 +49,7 @@ SocketNamespace.prototype.$emit = EventEmitter.prototype.emit; SocketNamespace.prototype.clients = function (room) { var room = this.name + (room !== undefined ? - (this.name !== '' ? '/' : '') + room : ''); + '/' + room : ''); if (!this.manager.rooms[room]) { return []; @@ -109,7 +109,7 @@ SocketNamespace.prototype.__defineGetter__('volatile', function () { */ SocketNamespace.prototype.in = function (room) { - this.flags.endpoint = (this.name === '' ? '' : (this.name + '/')) + room; + this.flags.endpoint = this.name + (room ? '/' + room : ''); return this; }; diff --git a/lib/socket.js b/lib/socket.js index a12b8f57e3..7a3b9e97e1 100644 --- a/lib/socket.js +++ b/lib/socket.js @@ -162,7 +162,7 @@ Socket.prototype.onDisconnect = function (reason) { Socket.prototype.join = function (name, fn) { var nsp = this.namespace.name - , name = (nsp === '' ? '' : (nsp + '/')) + name; + , name = (nsp + '/') + name; this.manager.onJoin(this.id, name); this.manager.store.publish('join', this.id, name); @@ -183,7 +183,7 @@ Socket.prototype.join = function (name, fn) { Socket.prototype.leave = function (name, fn) { var nsp = this.namespace.name - , name = (nsp === '' ? '' : (nsp + '/')) + name; + , name = (nsp + '/') + name; this.manager.onLeave(this.id, name); this.manager.store.publish('leave', this.id, name); diff --git a/test/namespace.test.js b/test/namespace.test.js index d2600e221f..5acb9d4bb6 100644 --- a/test/namespace.test.js +++ b/test/namespace.test.js @@ -139,5 +139,109 @@ module.exports = { } }) }); + }, + + 'broadcasting sends and emits on a namespace': function (done) { + var cl = client(++ports) + , io = create(cl) + , calls = 0 + , connect = 0 + , message = 0 + , events = 0 + , expected = 5 + , ws1 + , ws2; + + io.of('a') + .on('connection', function (socket){ + socket.broadcast.emit('b', 'test'); + socket.broadcast.json.emit('json', {foo:'bar'}); + socket.broadcast.send('foo'); + }); + + function finish () { + connect.should.equal(2); + message.should.equal(1); + events.should.equal(2); + + cl.end(); + ws1.finishClose(); + ws2.finishClose(); + io.server.close(); + done(); + } + + cl.handshake(function (sid) { + ws1 = websocket(cl, sid); + + ws1.on('open', function() { + ws1.packet({ + type: 'connect' + , endpoint: 'a' + }); + }); + + ws1.on('message', function (data) { + if (data.type === 'connect') { + ++connect; + if (++calls === expected) finish(); + } + + if (data.type === 'message') { + ++message; + if (++calls === expected) finish(); + } + + if (data.type === 'event') { + if (data.name === 'b' || data.name === 'json') ++events; + if (++calls === expected) finish(); + } + }); + + cl.handshake(function (sid) { + ws2 = websocket(cl, sid); + + ws2.on('open', function () { + ws2.packet({ + type: 'connect' + , endpoint: 'a' + }); + }); + }) + }) + }, + + 'joining rooms inside a namespace': function (done) { + var cl = client(++ports) + , io = create(cl) + , calls = 0 + , ws; + + io.of('/foo').on('connection', function (socket) { + socket.join('foo.bar'); + this.in('foo.bar').emit('baz', 'pewpew'); + }); + + cl.handshake(function (sid) { + ws = websocket(cl, sid); + + ws.on('open', function (){ + ws.packet({ + type: 'connect' + , endpoint: '/foo' + }); + }); + + ws.on('message', function (data) { + if (data.type === 'event') { + data.name.should.equal('baz'); + + cl.end(); + ws.finishClose(); + io.server.close(); + done(); + } + }); + }) } }; diff --git a/test/transports.websocket.test.js b/test/transports.websocket.test.js index 7293a67027..7bb3460792 100644 --- a/test/transports.websocket.test.js +++ b/test/transports.websocket.test.js @@ -12,7 +12,7 @@ var sio = require('socket.io') , should = require('./common') , parser = sio.parser - , ports = 15400; + , ports = 15800; /** * Tests.