Browse Source

Add ConnectionRefusedError and handling for it

pull/243/head
Andrey Rusanov 6 years ago
parent
commit
38edd2c939
  1. 16
      socketio/asyncio_server.py
  2. 14
      socketio/exceptions.py
  3. 15
      socketio/server.py

16
socketio/asyncio_server.py

@ -3,6 +3,7 @@ import asyncio
import engineio
from . import asyncio_manager
from . import exceptions
from . import packet
from . import server
@ -320,11 +321,18 @@ class AsyncServer(server.Server):
"""Handle a client connection request."""
namespace = namespace or '/'
self.manager.connect(sid, namespace)
if await self._trigger_event('connect', namespace, sid,
self.environ[sid]) is False:
try:
success = await self._trigger_event('connect', namespace, sid, self.environ[sid])
except exceptions.ConnectionRefusedError as exc:
fail_reason = exc.get_info()
success = False
else:
fail_reason = None
if success is False:
self.manager.disconnect(sid, namespace)
await self._send_packet(sid, packet.Packet(packet.ERROR,
namespace=namespace))
await self._send_packet(sid, packet.Packet(packet.ERROR, data=fail_reason, namespace=namespace))
if sid in self.environ: # pragma: no cover
del self.environ[sid]
return False

14
socketio/exceptions.py

@ -4,3 +4,17 @@ class SocketIOError(Exception):
class ConnectionError(SocketIOError):
pass
class ConnectionRefusedError(ConnectionError):
"""
Raised when connection is refused on the application level
"""
def __init__(self, info):
self._info = info
def get_info(self):
"""
This method could be overridden in subclass to add extra logic for data output
"""
return self._info

15
socketio/server.py

@ -4,6 +4,7 @@ import engineio
import six
from . import base_manager
from . import exceptions
from . import packet
from . import namespace
@ -485,11 +486,17 @@ class Server(object):
"""Handle a client connection request."""
namespace = namespace or '/'
self.manager.connect(sid, namespace)
if self._trigger_event('connect', namespace, sid,
self.environ[sid]) is False:
try:
success = self._trigger_event('connect', namespace, sid, self.environ[sid])
except exceptions.ConnectionRefusedError as exc:
fail_reason = exc.get_info()
success = False
else:
fail_reason = None
if success is False:
self.manager.disconnect(sid, namespace)
self._send_packet(sid, packet.Packet(packet.ERROR,
namespace=namespace))
self._send_packet(sid, packet.Packet(packet.ERROR, data=fail_reason, namespace=namespace))
if sid in self.environ: # pragma: no cover
del self.environ[sid]
return False

Loading…
Cancel
Save