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
4 changes: 2 additions & 2 deletions lib/namespace.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 [];
Expand Down Expand Up @@ -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;
};

Expand Down
4 changes: 2 additions & 2 deletions lib/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
104 changes: 104 additions & 0 deletions test/namespace.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
});
})
}
};
2 changes: 1 addition & 1 deletion test/transports.websocket.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
var sio = require('socket.io')
, should = require('./common')
, parser = sio.parser
, ports = 15400;
, ports = 15800;

/**
* Tests.
Expand Down