gateway.websocket: ignore compression when encoding=etf

This commit is contained in:
Luna 2020-01-25 00:07:31 -03:00
parent 6c802ad3fa
commit c8d37019a9
1 changed files with 10 additions and 12 deletions

View File

@ -173,26 +173,24 @@ class GatewayWebsocket:
payload.get("t"), payload.get("t"),
) )
# treat encoded as bytes if isinstance(encoded, str):
if not isinstance(encoded, bytes):
encoded = encoded.encode() encoded = encoded.encode()
if self.wsp.compress == "zlib-stream": if self.wsp.compress == "zlib-stream":
await self._zlib_stream_send(encoded) await self._zlib_stream_send(encoded)
elif self.wsp.compress == "zstd-stream": elif self.wsp.compress == "zstd-stream":
await self._zstd_stream_send(encoded) await self._zstd_stream_send(encoded)
elif self.state and self.state.compress and len(encoded) > 1024: elif (
# TODO: should we only compress on >1KB packets? or maybe we self.state
# should do all? and self.state.compress
and len(encoded) > 8192
and self.wsp.encoding != "etf"
):
# TODO determine better conditions to trigger a compress set
# by identify
await self.ws.send(zlib.compress(encoded)) await self.ws.send(zlib.compress(encoded))
else: else:
try: await self.ws.send(encoded)
# assume encoded is string, json based, decoding it
# should give reasonable messages down the websocket
await self.ws.send(encoded.decode())
except UnicodeDecodeError:
# in here, encoded is ETF, its bytes(), so we send it raw
await self.ws.send(encoded)
async def send_op(self, op_code: int, data: Any): async def send_op(self, op_code: int, data: Any):
"""Send a packet but just the OP code information is filled in.""" """Send a packet but just the OP code information is filled in."""