Skip to content

Commit a983c0b

Browse files
authored
fix!: check for a CLOSING_MESSAGE in the WebSocketClient connection (#682)
* fix!: listen for a `CLOSING_MESSAGE` * refactor: imports * fix: close check * fix: close check
1 parent ca49be1 commit a983c0b

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

interactions/api/gateway.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
from time import perf_counter
1818
from typing import Any, Dict, List, Optional, Tuple, Union
1919

20-
from aiohttp import WSMessage
21-
from aiohttp.http import WS_CLOSED_MESSAGE
20+
from aiohttp import WSMessage, WSMsgType
21+
from aiohttp.http import WS_CLOSED_MESSAGE, WS_CLOSING_MESSAGE
2222

2323
from ..base import get_logger
2424
from ..enums import InteractionType, OptionType
@@ -199,7 +199,7 @@ async def _establish_connection(
199199

200200
if stream is None:
201201
continue
202-
if self._client is None or stream == WS_CLOSED_MESSAGE:
202+
if self._client is None or stream == WS_CLOSED_MESSAGE or stream == WSMsgType.CLOSE:
203203
await self._establish_connection()
204204
break
205205

@@ -537,8 +537,18 @@ async def __receive_packet_stream(self) -> Optional[Dict[str, Any]]:
537537
"""
538538

539539
packet: WSMessage = await self._client.receive()
540-
if packet == WS_CLOSED_MESSAGE:
540+
541+
if packet == WSMsgType.CLOSE:
542+
await self._client.close()
543+
return packet
544+
545+
elif packet == WS_CLOSED_MESSAGE:
541546
return packet
547+
548+
elif packet == WS_CLOSING_MESSAGE:
549+
await self._client.close()
550+
return WS_CLOSED_MESSAGE
551+
542552
return loads(packet.data) if packet and isinstance(packet.data, str) else None
543553

544554
async def _send_packet(self, data: Dict[str, Any]) -> None:

0 commit comments

Comments
 (0)