async def _connect_session(self):
websocket_url, headers, cookies = self._get_connect_data()
try:
async with aiohttp.ClientSession(cookies=cookies) as session:
logger.debug(
f"Connecting to WebSocket using proxy {self._api.get_proxy().url}"
)
async with session.ws_connect(
websocket_url,
headers=headers,
proxy=self._api.get_proxy().url,
verify_ssl=False,
) as connection:
self.status = True
self._connection = connection
self._wait_connect_task.cancel()
self._matches_controller_task = asyncio.create_task(self._matches_controller())
logger.debug("Connected to websocket")
self.ping_task = asyncio.create_task(self._ping(connection))
await self._listen(connection)
logger.error(f"WsApp closed {self}")
self._destruct(WsClosed())
except Exception as e:
logger.exception(f"Exception in WsApp.connect {self}")
self._destruct(e)
async def _ping(self, connection: ClientWebSocketResponse):
while True:
try:
await asyncio.sleep(19)
await connection.send_str("2")
except Exception as e:
self._destruct(e)
async def _listen(self, connection: ClientWebSocketResponse):
while True:
msg = await connection.receive()
if msg.type == WSMsgType.TEXT:
data: str = msg.data
self._message_processor(data)
elif msg.type in (
WSMsgType.CLOSE,
WSMsgType.CLOSED,
WSMsgType.CLOSING,
WSMsgType.ERROR,
):
raise WsClosed()
def _destruct(self, err: BaseException):
self._callbacks.error_callback(self, err)
if self._run_task is not None:
self._run_task.cancel()
if self._matches_controller_task is not None:
self._matches_controller_task.cancel()
if self.ping_task is not None:
self.ping_task.cancel()
`
У меня есть этот код, и мое соединение продолжает закрываться, когда я пытаюсь отправить сообщение с помощью ping.
Почему не работает? t _listen получает сообщение о закрытии соединения?
В регистраторе я вижу только ошибки трассировки от _ping, без сообщений или ошибок от _listen
Traceback (most recent call last):
File "/opt/adapter/tipsport/tipsport_adapter/pipeline/single_match/data_providers/ws_provider/ws_app.py", line 139, in _ping
await connection.send_str("2")
File "/opt/adapter/.venv/lib/python3.11/site-packages/aiohttp/client_ws.py", line 225, in send_str
await self._writer.send(data, binary=False, compress=compress)
File "/opt/adapter/.venv/lib/python3.11/site-packages/aiohttp/http_websocket.py", line 726, in send
await self._send_frame(message, WSMsgType.TEXT, compress)
File "/opt/adapter/.venv/lib/python3.11/site-packages/aiohttp/http_websocket.py", line 626, in _send_frame
raise ConnectionResetError("Cannot write to closing transport")
ConnectionResetError: Cannot write to closing transport
`
Подробнее здесь: https://stackoverflow.com/questions/789 ... re-message