Handle a client connection request.
(self, eio_sid, namespace, data)
| 535 | await self.eio.send_packet(eio_sid, eio_pkt) |
| 536 | |
| 537 | async def _handle_connect(self, eio_sid, namespace, data): |
| 538 | """Handle a client connection request.""" |
| 539 | namespace = namespace or '/' |
| 540 | sid = None |
| 541 | if namespace in self.handlers or namespace in self.namespace_handlers \ |
| 542 | or self.namespaces == '*' or namespace in self.namespaces: |
| 543 | sid = await self.manager.connect(eio_sid, namespace) |
| 544 | if sid is None: |
| 545 | await self._send_packet(eio_sid, self.packet_class( |
| 546 | packet.CONNECT_ERROR, data='Unable to connect', |
| 547 | namespace=namespace)) |
| 548 | return |
| 549 | |
| 550 | if self.always_connect: |
| 551 | await self._send_packet(eio_sid, self.packet_class( |
| 552 | packet.CONNECT, {'sid': sid}, namespace=namespace)) |
| 553 | fail_reason = exceptions.ConnectionRefusedError().error_args |
| 554 | try: |
| 555 | if data: |
| 556 | success = await self._trigger_event( |
| 557 | 'connect', namespace, sid, self.environ[eio_sid], data) |
| 558 | else: |
| 559 | try: |
| 560 | success = await self._trigger_event( |
| 561 | 'connect', namespace, sid, self.environ[eio_sid]) |
| 562 | except TypeError: |
| 563 | success = await self._trigger_event( |
| 564 | 'connect', namespace, sid, self.environ[eio_sid], None) |
| 565 | except exceptions.ConnectionRefusedError as exc: |
| 566 | fail_reason = exc.error_args |
| 567 | success = False |
| 568 | except ConnectionRefusedError: |
| 569 | fail_reason = {"message": "Connection refused by server"} |
| 570 | success = False |
| 571 | |
| 572 | if success is False: |
| 573 | if self.always_connect: |
| 574 | self.manager.pre_disconnect(sid, namespace) |
| 575 | await self._send_packet(eio_sid, self.packet_class( |
| 576 | packet.DISCONNECT, data=fail_reason, namespace=namespace)) |
| 577 | else: |
| 578 | await self._send_packet(eio_sid, self.packet_class( |
| 579 | packet.CONNECT_ERROR, data=fail_reason, |
| 580 | namespace=namespace)) |
| 581 | await self.manager.disconnect(sid, namespace, ignore_queue=True) |
| 582 | elif not self.always_connect: |
| 583 | await self._send_packet(eio_sid, self.packet_class( |
| 584 | packet.CONNECT, {'sid': sid}, namespace=namespace)) |
| 585 | |
| 586 | async def _handle_disconnect(self, eio_sid, namespace, reason=None): |
| 587 | """Handle a client disconnect.""" |
no test coverage detected