Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to join several rooms at once #2879

Merged
merged 1 commit into from
Feb 26, 2017
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
9 changes: 9 additions & 0 deletions docs/API.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
- [socket.emit(eventName[, ...args][, ack])](#socketemiteventname-args-ack)
- [socket.on(eventName, callback)](#socketoneventname-callback)
- [socket.join(room[, callback])](#socketjoinroom-callback)
- [socket.join(rooms[, callback])](#socketjoinrooms-callback)
- [socket.leave(room[, callback])](#socketleaveroom-callback)
- [socket.to(room)](#sockettoroom)
- [socket.in(room)](#socketinroom)
Expand Down Expand Up @@ -476,6 +477,14 @@ io.on('connection', function(client){
});
```

#### socket.join(rooms[, callback])

- `rooms` _(Array)_
- `callback` _(Function)_
- **Returns** `Socket` for chaining

Adds the client to the list of room, and fires optionally a callback with `err` signature (if any).

#### socket.leave(room[, callback])

- `room` _(String)_
Expand Down
22 changes: 15 additions & 7 deletions lib/socket.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,23 +223,31 @@ Socket.prototype.packet = function(packet, opts){
/**
* Joins a room.
*
* @param {String} room
* @param {String|Array} room or array of rooms
* @param {Function} fn optional, callback
* @return {Socket} self
* @api private
*/

Socket.prototype.join = function(room, fn){
debug('joining room %s', room);
Socket.prototype.join = function(rooms, fn){
debug('joining room %s', rooms);
var self = this;
if (this.rooms.hasOwnProperty(room)) {
if (!Array.isArray(rooms)) {
rooms = [rooms];
}
rooms = rooms.filter(function (room) {
return !self.rooms.hasOwnProperty(room);
});
if (!rooms.length) {
fn && fn(null);
return this;
}
this.adapter.add(this.id, room, function(err){
this.adapter.addAll(this.id, rooms, function(err){
if (err) return fn && fn(err);
debug('joined room %s', room);
self.rooms[room] = room;
debug('joined room %s', rooms);
rooms.forEach(function (room) {
self.rooms[room] = room;
});
fn && fn(null);
});
return this;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"engine.io": "2.0.2",
"has-binary": "0.1.7",
"object-assign": "4.1.0",
"socket.io-adapter": "1.0.0",
"socket.io-adapter": "~1.1.0",
"socket.io-client": "socketio/socket.io-client",
"socket.io-parser": "2.3.1"
},
Expand Down
15 changes: 15 additions & 0 deletions test/socket.io.js
Original file line number Diff line number Diff line change
Expand Up @@ -2083,6 +2083,21 @@ describe('socket.io', function(){
});
});
});

it('allows to join several rooms at once', function(done) {
var srv = http();
var sio = io(srv);

srv.listen(function(){
var socket = client(srv);
sio.on('connection', function(s){
s.join(['a', 'b', 'c'], function(){
expect(Object.keys(s.rooms)).to.eql([s.id, 'a', 'b', 'c']);
done();
});
});
});
});
});

describe('middleware', function(done){
Expand Down