Skip to content

Commit

Permalink
Update WebSocket sender to avoid extra utf-8 decode/encode round-trip (
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored Nov 14, 2024
1 parent 4aee4d1 commit d9e91b6
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
9 changes: 9 additions & 0 deletions aioshelly/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,12 @@ def json_dumps(data: Any) -> str:
option=orjson.OPT_NON_STR_KEYS,
default=json_encoder_default,
).decode("utf-8")


def json_bytes(data: Any) -> bytes:
"""Dump json bytes."""
return orjson.dumps(
data,
option=orjson.OPT_NON_STR_KEYS,
default=json_encoder_default,
)
4 changes: 2 additions & 2 deletions aioshelly/rpc_device/wsrpc.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
InvalidMessage,
RpcCallError,
)
from ..json import json_dumps, json_loads
from ..json import json_bytes, json_loads

_LOGGER = logging.getLogger(__name__)

Expand Down Expand Up @@ -531,7 +531,7 @@ async def _send_json(self, data: dict[str, Any]) -> None:
if TYPE_CHECKING:
assert self._client

await self._client.send_str(json_dumps(data))
await self._client.send_frame(json_bytes(data), WSMsgType.TEXT)


class WsServer(WsBase):
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
bluetooth-data-tools>=1.19.0
aiohttp
aiohttp>=3.11.1
habluetooth>=2.1.0
yarl
orjson>=3.8.1

0 comments on commit d9e91b6

Please sign in to comment.