Browse Source

skip_sid parameter can also be a list (fixes #202)

pull/319/head
Miguel Grinberg 6 years ago
parent
commit
00d39ca698
No known key found for this signature in database GPG Key ID: 36848B262DF5F06C
  1. 4
      socketio/asyncio_manager.py
  2. 4
      socketio/base_manager.py
  3. 6
      socketio/server.py
  4. 13
      tests/asyncio/test_asyncio_manager.py
  5. 14
      tests/common/test_base_manager.py

4
socketio/asyncio_manager.py

@ -15,8 +15,10 @@ class AsyncManager(BaseManager):
if namespace not in self.rooms or room not in self.rooms[namespace]: if namespace not in self.rooms or room not in self.rooms[namespace]:
return return
tasks = [] tasks = []
if not isinstance(skip_sid, list):
skip_sid = [skip_sid]
for sid in self.get_participants(namespace, room): for sid in self.get_participants(namespace, room):
if sid != skip_sid: if sid not in skip_sid:
if callback is not None: if callback is not None:
id = self._generate_ack_id(sid, namespace, callback) id = self._generate_ack_id(sid, namespace, callback)
else: else:

4
socketio/base_manager.py

@ -130,8 +130,10 @@ class BaseManager(object):
connected to the namespace.""" connected to the namespace."""
if namespace not in self.rooms or room not in self.rooms[namespace]: if namespace not in self.rooms or room not in self.rooms[namespace]:
return return
if not isinstance(skip_sid, list):
skip_sid = [skip_sid]
for sid in self.get_participants(namespace, room): for sid in self.get_participants(namespace, room):
if sid != skip_sid: if sid not in skip_sid:
if callback is not None: if callback is not None:
id = self._generate_ack_id(sid, namespace, callback) id = self._generate_ack_id(sid, namespace, callback)
else: else:

6
socketio/server.py

@ -218,7 +218,8 @@ class Server(object):
connected clients. connected clients.
:param skip_sid: The session ID of a client to skip when broadcasting :param skip_sid: The session ID of a client to skip when broadcasting
to a room or to all clients. This can be used to to a room or to all clients. This can be used to
prevent a message from being sent to the sender. prevent a message from being sent to the sender. To
skip multiple sids, pass a list.
:param namespace: The Socket.IO namespace for the event. If this :param namespace: The Socket.IO namespace for the event. If this
argument is omitted the event is emitted to the argument is omitted the event is emitted to the
default namespace. default namespace.
@ -258,7 +259,8 @@ class Server(object):
connected clients. connected clients.
:param skip_sid: The session ID of a client to skip when broadcasting :param skip_sid: The session ID of a client to skip when broadcasting
to a room or to all clients. This can be used to to a room or to all clients. This can be used to
prevent a message from being sent to the sender. prevent a message from being sent to the sender. To
skip multiple sids, pass a list.
:param namespace: The Socket.IO namespace for the event. If this :param namespace: The Socket.IO namespace for the event. If this
argument is omitted the event is emitted to the argument is omitted the event is emitted to the
default namespace. default namespace.

13
tests/asyncio/test_asyncio_manager.py

@ -255,6 +255,19 @@ class TestAsyncManager(unittest.TestCase):
self.bm.server._emit_internal.mock.assert_any_call( self.bm.server._emit_internal.mock.assert_any_call(
'789', 'my event', {'foo': 'bar'}, '/foo', None) '789', 'my event', {'foo': 'bar'}, '/foo', None)
def test_emit_to_all_skip_two(self):
self.bm.connect('123', '/foo')
self.bm.enter_room('123', '/foo', 'bar')
self.bm.connect('456', '/foo')
self.bm.enter_room('456', '/foo', 'bar')
self.bm.connect('789', '/foo')
self.bm.connect('abc', '/bar')
_run(self.bm.emit('my event', {'foo': 'bar'}, namespace='/foo',
skip_sid=['123', '789']))
self.assertEqual(self.bm.server._emit_internal.mock.call_count, 1)
self.bm.server._emit_internal.mock.assert_any_call(
'456', 'my event', {'foo': 'bar'}, '/foo', None)
def test_emit_with_callback(self): def test_emit_with_callback(self):
self.bm.connect('123', '/foo') self.bm.connect('123', '/foo')
self.bm._generate_ack_id = mock.MagicMock() self.bm._generate_ack_id = mock.MagicMock()

14
tests/common/test_base_manager.py

@ -237,6 +237,20 @@ class TestBaseManager(unittest.TestCase):
{'foo': 'bar'}, '/foo', {'foo': 'bar'}, '/foo',
None) None)
def test_emit_to_all_skip_two(self):
self.bm.connect('123', '/foo')
self.bm.enter_room('123', '/foo', 'bar')
self.bm.connect('456', '/foo')
self.bm.enter_room('456', '/foo', 'bar')
self.bm.connect('789', '/foo')
self.bm.connect('abc', '/bar')
self.bm.emit('my event', {'foo': 'bar'}, namespace='/foo',
skip_sid=['123', '789'])
self.assertEqual(self.bm.server._emit_internal.call_count, 1)
self.bm.server._emit_internal.assert_any_call('456', 'my event',
{'foo': 'bar'}, '/foo',
None)
def test_emit_with_callback(self): def test_emit_with_callback(self):
self.bm.connect('123', '/foo') self.bm.connect('123', '/foo')
self.bm._generate_ack_id = mock.MagicMock() self.bm._generate_ack_id = mock.MagicMock()

Loading…
Cancel
Save