Browse Source

Handle `to` in async manager and sync versions

pull/1374/head
Pavel Mikhalkevich 8 months ago
parent
commit
d5246982b9
  1. 3
      src/socketio/async_manager.py
  2. 3
      src/socketio/manager.py
  3. 3
      src/socketio/pubsub_manager.py
  4. 2
      tests/async/test_manager.py
  5. 2
      tests/common/test_manager.py
  6. 16
      tests/common/test_pubsub_manager.py

3
src/socketio/async_manager.py

@ -11,12 +11,13 @@ class AsyncManager(BaseManager):
return self.is_connected(sid, namespace)
async def emit(self, event, data, namespace, room=None, skip_sid=None,
callback=None, **kwargs):
callback=None, to=None, **kwargs):
"""Emit a message to a single client, a room, or all the clients
connected to the namespace.
Note: this method is a coroutine.
"""
room = to or room
if namespace not in self.rooms:
return
if isinstance(data, tuple):

3
src/socketio/manager.py

@ -20,9 +20,10 @@ class Manager(base_manager.BaseManager):
return self.is_connected(sid, namespace)
def emit(self, event, data, namespace, room=None, skip_sid=None,
callback=None, **kwargs):
callback=None, to=None, **kwargs):
"""Emit a message to a single client, a room, or all the clients
connected to the namespace."""
room = to or room
if namespace not in self.rooms:
return
if isinstance(data, tuple):

3
src/socketio/pubsub_manager.py

@ -37,7 +37,7 @@ class PubSubManager(Manager):
self._get_logger().info(self.name + ' backend initialized.')
def emit(self, event, data, namespace=None, room=None, skip_sid=None,
callback=None, **kwargs):
callback=None, to=None, **kwargs):
"""Emit a message to a single client, a room, or all the clients
connected to the namespace.
@ -46,6 +46,7 @@ class PubSubManager(Manager):
The parameters are the same as in :meth:`.Server.emit`.
"""
room = to or room
if kwargs.get('ignore_queue'):
return super().emit(
event, data, namespace=namespace, room=room, skip_sid=skip_sid,

2
tests/async/test_manager.py

@ -205,7 +205,7 @@ class TestAsyncManager(unittest.TestCase):
_run(self.bm.connect('456', '/foo'))
_run(
self.bm.emit(
'my event', {'foo': 'bar'}, namespace='/foo', room=sid
'my event', {'foo': 'bar'}, namespace='/foo', to=sid
)
)
assert self.bm.server._send_eio_packet.mock.call_count == 1

2
tests/common/test_manager.py

@ -206,7 +206,7 @@ class TestBaseManager(unittest.TestCase):
def test_emit_to_sid(self):
sid = self.bm.connect('123', '/foo')
self.bm.connect('456', '/foo')
self.bm.emit('my event', {'foo': 'bar'}, namespace='/foo', room=sid)
self.bm.emit('my event', {'foo': 'bar'}, namespace='/foo', to=sid)
assert self.bm.server._send_eio_packet.call_count == 1
assert self.bm.server._send_eio_packet.call_args_list[0][0][0] == '123'
pkt = self.bm.server._send_eio_packet.call_args_list[0][0][1]

16
tests/common/test_pubsub_manager.py

@ -78,6 +78,22 @@ class TestPubSubManager(unittest.TestCase):
}
)
def test_emit_with_to(self):
sid = "ferris"
self.pm.emit('foo', 'bar', to=sid)
self.pm._publish.assert_called_once_with(
{
'method': 'emit',
'event': 'foo',
'data': 'bar',
'namespace': '/',
'room': sid,
'skip_sid': None,
'callback': None,
'host_id': '123456',
}
)
def test_emit_with_namespace(self):
self.pm.emit('foo', 'bar', namespace='/baz')
self.pm._publish.assert_called_once_with(

Loading…
Cancel
Save