Skip to content

Commit

Permalink
Return the rooms a client is in
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelgrinberg committed Jul 20, 2015
1 parent 7195217 commit d4cd9de
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 0 deletions.
8 changes: 8 additions & 0 deletions socketio/base_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,14 @@ def close_room(self, namespace, room):
except KeyError:
pass

def get_rooms(self, sid, namespace):
"""Return the rooms a client is in."""
r = []
for room_name, room in six.iteritems(self.rooms[namespace]):
if room_name is not None and sid in room and room[sid]:
r.append(room_name)
return r

def emit(self, event, data, namespace, room=None, skip_sid=None,
callback=None):
"""Emit a message to a single client, a room, or all the clients
Expand Down
10 changes: 10 additions & 0 deletions socketio/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,16 @@ def close_room(self, room, namespace=None):
self.logger.info('room %s is closing [%s]', room, namespace)
self.manager.close_room(namespace, room)

def rooms(self, sid, namespace=None):
"""Return the rooms a client is in.
:param sid: Session ID of the client.
:param namespace: The Socket.IO namespace for the event. If this
argument is omitted the default namespace is used.
"""
namespace = namespace or '/'
return self.manager.get_rooms(sid, namespace)

def handle_request(self, environ, start_response):
"""Handle an HTTP request from the client.
Expand Down
8 changes: 8 additions & 0 deletions tests/test_base_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,14 @@ def test_close_room(self):
def test_close_invalid_room(self):
self.bm.close_room('/foo', 'bar')

def test_rooms(self):
self.bm.connect('123', '/foo')
self.bm.enter_room('123', '/foo', 'bar')
r = self.bm.get_rooms('123', '/foo')
self.assertEqual(len(r), 2)
self.assertIn('123', r)
self.assertIn('bar', r)

def test_emit_to_sid(self):
self.bm.connect('123', '/foo')
self.bm.connect('456', '/foo')
Expand Down
12 changes: 12 additions & 0 deletions tests/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,18 @@ def test_close_room_default_namespace(self, eio):
s.close_room('room')
s.manager.close_room.assert_called_once_with('/', 'room')

def test_rooms(self, eio):
mgr = mock.MagicMock()
s = server.Server(client_manager_class=mgr)
s.rooms('123', namespace='/foo')
s.manager.get_rooms.assert_called_once_with('123', '/foo')

def test_rooms_default_namespace(self, eio):
mgr = mock.MagicMock()
s = server.Server(client_manager_class=mgr)
s.rooms('123')
s.manager.get_rooms.assert_called_once_with('123', '/')

def test_handle_request(self, eio):
s = server.Server()
s.handle_request('environ', 'start_response')
Expand Down

0 comments on commit d4cd9de

Please sign in to comment.