Run the WebSocket ASGI application.
(self)
| 98 | self._data_event.set() |
| 99 | |
| 100 | async def run(self): |
| 101 | """Run the WebSocket ASGI application.""" |
| 102 | # Send initial connect event |
| 103 | await self._receive_queue.put({"type": "websocket.connect"}) |
| 104 | |
| 105 | # Start frame reading task |
| 106 | read_task = asyncio.create_task(self._read_frames()) |
| 107 | |
| 108 | try: |
| 109 | await self.app(self.scope, self._receive, self._send) |
| 110 | except Exception: |
| 111 | self.log.exception("Error in WebSocket ASGI application") |
| 112 | finally: |
| 113 | # Send close frame if not already closed |
| 114 | if not self.closed and self.accepted and not self._close_sent: |
| 115 | await self._send_close(CLOSE_INTERNAL_ERROR, "Application error") |
| 116 | # Wait for client's close response |
| 117 | try: |
| 118 | await asyncio.wait_for(self._close_event.wait(), timeout=5.0) |
| 119 | except asyncio.TimeoutError: |
| 120 | self.closed = True |
| 121 | |
| 122 | read_task.cancel() |
| 123 | try: |
| 124 | await read_task |
| 125 | except asyncio.CancelledError: |
| 126 | pass |
| 127 | |
| 128 | async def _receive(self): |
| 129 | """ASGI receive callable.""" |
no test coverage detected