From 1a2c8aa31fcd692d49e2a1f0633d29845fac313d Mon Sep 17 00:00:00 2001 From: Arnout Kazemier Date: Tue, 5 Jul 2011 00:12:59 +0200 Subject: [PATCH 1/5] It was emitting uknown room, so the messages where never send fixes #291 --- lib/namespace.js | 2 +- test/namespace.test.js | 68 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 1 deletion(-) diff --git a/lib/namespace.js b/lib/namespace.js index db7db2cca2..7fb5f2c5cc 100644 --- a/lib/namespace.js +++ b/lib/namespace.js @@ -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; return this; }; diff --git a/test/namespace.test.js b/test/namespace.test.js index d2600e221f..7eaa096be9 100644 --- a/test/namespace.test.js +++ b/test/namespace.test.js @@ -139,5 +139,73 @@ 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' + }); + }); + }) + }) } }; From a70347b15ff1db01d95f3c7ebc79c48ad8e5c6b5 Mon Sep 17 00:00:00 2001 From: Arnout Kazemier Date: Tue, 5 Jul 2011 00:37:19 +0200 Subject: [PATCH 2/5] Fixed #285 --- lib/namespace.js | 2 +- test/namespace.test.js | 41 +++++++++++++++++++++++++++++++++++++++-- 2 files changed, 40 insertions(+), 3 deletions(-) diff --git a/lib/namespace.js b/lib/namespace.js index 7fb5f2c5cc..90be93122d 100644 --- a/lib/namespace.js +++ b/lib/namespace.js @@ -109,7 +109,7 @@ SocketNamespace.prototype.__defineGetter__('volatile', function () { */ SocketNamespace.prototype.in = function (room) { - this.flags.endpoint = this.name + room; + this.flags.endpoint = this.name + (room ? '/' + room : ''); return this; }; diff --git a/test/namespace.test.js b/test/namespace.test.js index 7eaa096be9..f8668f7d1a 100644 --- a/test/namespace.test.js +++ b/test/namespace.test.js @@ -173,6 +173,7 @@ module.exports = { cl.handshake(function (sid) { ws1 = websocket(cl, sid); + ws1.on('open', function() { ws1.packet({ type: 'connect' @@ -180,7 +181,7 @@ module.exports = { }); }); - ws1.on('message', function(data) { + ws1.on('message', function (data) { if (data.type === 'connect') { ++connect; if (++calls === expected) finish(); @@ -199,7 +200,8 @@ module.exports = { cl.handshake(function (sid) { ws2 = websocket(cl, sid); - ws2.on('open', function() { + + ws2.on('open', function () { ws2.packet({ type: 'connect' , endpoint: 'a' @@ -207,5 +209,40 @@ module.exports = { }); }) }) + }, + + 'joining rooms inside a namespace': function (done) { + var cl = client(++ports) + , io = create(cl) + , calls = 0 + console.log(data) + , 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(); + } + }); + }) } }; From 159c75096dee45f9583c864fa8deec7a2b23a314 Mon Sep 17 00:00:00 2001 From: Arnout Kazemier Date: Tue, 5 Jul 2011 00:38:24 +0200 Subject: [PATCH 3/5] Removed console statement --- test/namespace.test.js | 1 - 1 file changed, 1 deletion(-) diff --git a/test/namespace.test.js b/test/namespace.test.js index f8668f7d1a..5acb9d4bb6 100644 --- a/test/namespace.test.js +++ b/test/namespace.test.js @@ -215,7 +215,6 @@ module.exports = { var cl = client(++ports) , io = create(cl) , calls = 0 - console.log(data) , ws; io.of('/foo').on('connection', function (socket) { From d39d1401c4785787ecfa61162a086784934d92bc Mon Sep 17 00:00:00 2001 From: Arnout Kazemier Date: Tue, 5 Jul 2011 00:59:31 +0200 Subject: [PATCH 4/5] Fixed broken test suite --- lib/namespace.js | 2 +- lib/socket.js | 4 ++-- test/transports.websocket.test.js | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/namespace.js b/lib/namespace.js index 90be93122d..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 []; 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/transports.websocket.test.js b/test/transports.websocket.test.js index 7293a67027..62493b06ac 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 = 225400; /** * Tests. From 81d71ebb08aa2577530bbe6d00d94456f919ea19 Mon Sep 17 00:00:00 2001 From: Arnout Kazemier Date: Tue, 12 Jul 2011 09:52:53 +0200 Subject: [PATCH 5/5] Fixed port number --- test/transports.websocket.test.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/transports.websocket.test.js b/test/transports.websocket.test.js index 62493b06ac..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 = 225400; + , ports = 15800; /** * Tests.