@@ -149,9 +149,9 @@ async def _receive_and_check(self, timeout: typing.Optional[float], /) -> str:
149149 self ._logger .error ("connection closed with code %s (%s)" , close_code , reason )
150150
151151 can_reconnect = close_code < 4000 or close_code in (
152+ errors .ShardCloseCode .UNKNOWN_ERROR ,
152153 errors .ShardCloseCode .DECODE_ERROR ,
153154 errors .ShardCloseCode .INVALID_SEQ ,
154- errors .ShardCloseCode .UNKNOWN_ERROR ,
155155 errors .ShardCloseCode .SESSION_TIMEOUT ,
156156 errors .ShardCloseCode .RATE_LIMITED ,
157157 )
@@ -162,13 +162,18 @@ async def _receive_and_check(self, timeout: typing.Optional[float], /) -> str:
162162 elif message .type == aiohttp .WSMsgType .CLOSING or message .type == aiohttp .WSMsgType .CLOSED :
163163 raise asyncio .CancelledError ("Socket closed" )
164164
165+ elif len (buff ) != 0 and message .type != aiohttp .WSMsgType .BINARY :
166+ raise errors .GatewayError (f"Unexpected message type received { message .type .name } , expected BINARY" )
167+
165168 elif message .type == aiohttp .WSMsgType .BINARY :
166- buff += message .data
169+ buff . extend ( message .data )
167170
168171 if buff .endswith (b"\x00 \x00 \xff \xff " ):
169172 return self ._zlib .decompress (buff ).decode ("utf-8" )
173+
170174 elif message .type == aiohttp .WSMsgType .TEXT :
171175 return message .data # type: ignore
176+
172177 else :
173178 # Assume exception for now.
174179 ex = self .exception ()
@@ -192,8 +197,8 @@ async def connect(
192197 * ,
193198 debug : bool ,
194199 http_config : config .HTTPSettings ,
195- logger : logging .Logger ,
196200 proxy_config : config .ProxySettings ,
201+ logger : logging .Logger ,
197202 url : str ,
198203 ) -> typing .AsyncGenerator [_V6GatewayTransport , None ]:
199204 """Generate a single-use websocket connection.
@@ -809,9 +814,10 @@ async def _run_once(self) -> bool:
809814 )
810815 return True
811816 return False
812- finally :
813817
818+ finally :
814819 heartbeat_task .cancel ()
820+
815821 finally :
816822 self ._ws = None
817823 if dispatch_disconnect :
0 commit comments