Browse Source

Fixed disconnect implementation when using a message queue (Fixes #1002)

pull/1056/head
Miguel Grinberg 3 years ago
parent
commit
f56ef6f040
No known key found for this signature in database GPG Key ID: 36848B262DF5F06C
  1. 2
      src/socketio/base_manager.py
  2. 5
      src/socketio/pubsub_manager.py
  3. 7
      src/socketio/server.py
  4. 7
      tests/common/test_pubsub_manager.py
  5. 3
      tests/common/test_server.py

2
src/socketio/base_manager.py

@ -94,7 +94,7 @@ class BaseManager(object):
self.pending_disconnect[namespace].append(sid)
return self.rooms[namespace][None].get(sid)
def disconnect(self, sid, namespace):
def disconnect(self, sid, namespace, **kwargs):
"""Register a client disconnect from a namespace."""
if namespace not in self.rooms:
return

5
src/socketio/pubsub_manager.py

@ -75,7 +75,10 @@ class PubSubManager(BaseManager):
self._publish({'method': 'disconnect', 'sid': sid,
'namespace': namespace or '/'})
def disconnect(self, sid, namespace=None):
def disconnect(self, sid, namespace=None, **kwargs):
if kwargs.get('ignore_queue'):
return super(PubSubManager, self).disconnect(
sid, namespace=namespace)
self._publish({'method': 'disconnect', 'sid': sid,
'namespace': namespace or '/'})

7
src/socketio/server.py

@ -565,7 +565,8 @@ class Server(object):
self._send_packet(eio_sid, self.packet_class(
packet.DISCONNECT, namespace=namespace))
self._trigger_event('disconnect', namespace, sid)
self.manager.disconnect(sid, namespace=namespace)
self.manager.disconnect(sid, namespace=namespace,
ignore_queue=True)
def transport(self, sid):
"""Return the name of the transport used by the client.
@ -693,7 +694,7 @@ class Server(object):
self._send_packet(eio_sid, self.packet_class(
packet.CONNECT_ERROR, data=fail_reason,
namespace=namespace))
self.manager.disconnect(sid, namespace)
self.manager.disconnect(sid, namespace, ignore_queue=True)
elif not self.always_connect:
self._send_packet(eio_sid, self.packet_class(
packet.CONNECT, {'sid': sid}, namespace=namespace))
@ -706,7 +707,7 @@ class Server(object):
return
self.manager.pre_disconnect(sid, namespace=namespace)
self._trigger_event('disconnect', namespace, sid)
self.manager.disconnect(sid, namespace)
self.manager.disconnect(sid, namespace, ignore_queue=True)
def _handle_event(self, eio_sid, namespace, id, data):
"""Handle an incoming client event."""

7
tests/common/test_pubsub_manager.py

@ -175,6 +175,13 @@ class TestPubSubManager(unittest.TestCase):
{'method': 'disconnect', 'sid': 'foo', 'namespace': '/'}
)
def test_disconnect_ignore_queue(self):
sid = self.pm.connect('123', '/')
self.pm.pre_disconnect(sid, '/')
self.pm.disconnect(sid, ignore_queue=True)
self.pm._publish.assert_not_called()
assert not self.pm.is_connected(sid, '/')
def test_close_room(self):
self.pm.close_room('foo')
self.pm._publish.assert_called_once_with(

3
tests/common/test_server.py

@ -532,7 +532,8 @@ class TestServer(unittest.TestCase):
s._handle_eio_message('123', '0')
s._handle_eio_disconnect('123')
handler.assert_called_once_with('1')
s.manager.disconnect.assert_called_once_with('1', '/')
s.manager.disconnect.assert_called_once_with('1', '/',
ignore_queue=True)
assert s.environ == {}
def test_handle_disconnect_namespace(self, eio):

Loading…
Cancel
Save