Browse Source

Return the rooms a client is in

pull/5/head
Miguel Grinberg 10 years ago
parent
commit
d4cd9de799
  1. 8
      socketio/base_manager.py
  2. 10
      socketio/server.py
  3. 8
      tests/test_base_manager.py
  4. 12
      tests/test_server.py

8
socketio/base_manager.py

@ -71,6 +71,14 @@ class BaseManager(object):
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

10
socketio/server.py

@ -236,6 +236,16 @@ class Server(object):
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.

8
tests/test_base_manager.py

@ -107,6 +107,14 @@ class TestBaseManager(unittest.TestCase):
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')

12
tests/test_server.py

@ -94,6 +94,18 @@ class TestServer(unittest.TestCase):
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')

Loading…
Cancel
Save