diff --git a/src/socketio/asyncio_server.py b/src/socketio/asyncio_server.py index 8dbba22..3898543 100644 --- a/src/socketio/asyncio_server.py +++ b/src/socketio/asyncio_server.py @@ -450,14 +450,17 @@ class AsyncServer(server.Server): try: if data: success = await self._trigger_event( - 'connect', namespace, sid, self.environ[eio_sid], data, safe=False) + 'connect', namespace, sid, + self.environ[eio_sid], data, safe=False) else: try: success = await self._trigger_event( - 'connect', namespace, sid, self.environ[eio_sid], safe=False) + 'connect', namespace, sid, + self.environ[eio_sid], safe=False) except TypeError: success = await self._trigger_event( - 'connect', namespace, sid, self.environ[eio_sid], None, safe=False) + 'connect', namespace, sid, + self.environ[eio_sid], None, safe=False) except exceptions.ConnectionRefusedError as exc: fail_reason = exc.error_args success = False @@ -507,7 +510,8 @@ class AsyncServer(server.Server): async def _handle_event_internal(self, server, sid, eio_sid, data, namespace, id): - r = await server._trigger_event(data[0], namespace, sid, *data[1:], safe=True) + r = await server._trigger_event(data[0], namespace, sid, *data[1:], + safe=True) if id is not None: # send ACK packet with the response returned by the handler # tuples are expanded as multiple arguments diff --git a/src/socketio/exceptions.py b/src/socketio/exceptions.py index dcb92e4..cfefe5e 100644 --- a/src/socketio/exceptions.py +++ b/src/socketio/exceptions.py @@ -33,5 +33,6 @@ class TimeoutError(SocketIOError): class BadNamespaceError(SocketIOError): pass + class NamespaceNotFoundError(SocketIOError): pass diff --git a/src/socketio/server.py b/src/socketio/server.py index 9ffc2bc..cea8782 100644 --- a/src/socketio/server.py +++ b/src/socketio/server.py @@ -158,6 +158,7 @@ class Server(object): self.always_connect = always_connect self.async_mode = self.eio.async_mode + self.on('connect', namespace='/', handler=lambda sid, environ: True) def is_asyncio_based(self): return False @@ -662,14 +663,17 @@ class Server(object): try: if data: success = self._trigger_event( - 'connect', namespace, sid, self.environ[eio_sid], data, safe=False) + 'connect', namespace, sid, + self.environ[eio_sid], data, safe=False) else: try: success = self._trigger_event( - 'connect', namespace, sid, self.environ[eio_sid], safe=False) + 'connect', namespace, sid, + self.environ[eio_sid], safe=False) except TypeError: success = self._trigger_event( - 'connect', namespace, sid, self.environ[eio_sid], None, safe=False) + 'connect', namespace, sid, + self.environ[eio_sid], None, safe=False) except exceptions.ConnectionRefusedError as exc: fail_reason = exc.error_args success = False @@ -720,7 +724,8 @@ class Server(object): def _handle_event_internal(self, server, sid, eio_sid, data, namespace, id): - r = server._trigger_event(data[0], namespace, sid, *data[1:], safe=True) + r = server._trigger_event(data[0], namespace, sid, *data[1:], + safe=True) if id is not None: # send ACK packet with the response returned by the handler # tuples are expanded as multiple arguments diff --git a/tests/asyncio/test_asyncio_server.py b/tests/asyncio/test_asyncio_server.py index 824d4a5..a899e72 100644 --- a/tests/asyncio/test_asyncio_server.py +++ b/tests/asyncio/test_asyncio_server.py @@ -765,6 +765,11 @@ class TestAsyncServer(unittest.TestCase): def test_send_with_ack_namespace(self, eio): eio.return_value.send = AsyncMock() s = asyncio_server.AsyncServer() + + class FooNamespace(asyncio_namespace.AsyncNamespace): + pass + + s.register_namespace(FooNamespace("/foo")) _run(s._handle_eio_connect('123', 'environ')) _run(s._handle_eio_message('123', '0/foo,')) cb = mock.MagicMock() @@ -791,6 +796,11 @@ class TestAsyncServer(unittest.TestCase): eio.return_value.send = AsyncMock() s = asyncio_server.AsyncServer() + + class MyNamespace(asyncio_namespace.AsyncNamespace): + pass + + s.register_namespace(MyNamespace("/ns")) s.eio.get_session = fake_get_session s.eio.save_session = fake_save_session @@ -842,6 +852,11 @@ class TestAsyncServer(unittest.TestCase): eio.return_value.send = AsyncMock() eio.return_value.disconnect = AsyncMock() s = asyncio_server.AsyncServer() + + class FooNamespace(asyncio_namespace.AsyncNamespace): + pass + + s.register_namespace(FooNamespace("/foo")) _run(s._handle_eio_connect('123', 'environ')) _run(s._handle_eio_message('123', '0/foo,')) _run(s.disconnect('1', namespace='/foo')) diff --git a/tests/common/test_server.py b/tests/common/test_server.py index 3b89c3b..d0e5342 100644 --- a/tests/common/test_server.py +++ b/tests/common/test_server.py @@ -675,6 +675,11 @@ class TestServer(unittest.TestCase): def test_send_with_ack_namespace(self, eio): s = server.Server() + + class FooNamespace(namespace.Namespace): + pass + + s.register_namespace(FooNamespace("/foo")) s._handle_eio_connect('123', 'environ') s._handle_eio_message('123', '0/foo,') cb = mock.MagicMock() @@ -696,6 +701,11 @@ class TestServer(unittest.TestCase): fake_session = session s = server.Server() + + class MyNamespace(namespace.Namespace): + pass + + s.register_namespace(MyNamespace("/ns")) s.eio.get_session = fake_get_session s.eio.save_session = fake_save_session s._handle_eio_connect('123', 'environ') @@ -735,6 +745,11 @@ class TestServer(unittest.TestCase): def test_disconnect_namespace(self, eio): s = server.Server() + + class FooNamespace(namespace.Namespace): + pass + + s.register_namespace(FooNamespace("/foo")) s._handle_eio_connect('123', 'environ') s._handle_eio_message('123', '0/foo,') s.disconnect('1', namespace='/foo')