1
0
mirror of https://github.com/home-assistant/core.git synced 2025-12-24 12:59:34 +00:00

Better handle large amounts of data being sent over WS (#23842)

* Better handle large amounts of data being sent over WS

* Lint
This commit is contained in:
Paulus Schoutsen
2019-05-14 05:57:47 +02:00
committed by GitHub
parent b2a1204bc5
commit 45085dd97f
7 changed files with 59 additions and 20 deletions

View File

@@ -1,8 +1,6 @@
"""View to accept incoming websocket connection."""
import asyncio
from contextlib import suppress
from functools import partial
import json
import logging
from aiohttp import web, WSMsgType
@@ -11,18 +9,15 @@ import async_timeout
from homeassistant.const import EVENT_HOMEASSISTANT_STOP
from homeassistant.core import callback
from homeassistant.components.http import HomeAssistantView
from homeassistant.helpers.json import JSONEncoder
from .const import (
MAX_PENDING_MSG, CANCELLATION_ERRORS, URL, ERR_UNKNOWN_ERROR,
SIGNAL_WEBSOCKET_CONNECTED, SIGNAL_WEBSOCKET_DISCONNECTED,
DATA_CONNECTIONS)
DATA_CONNECTIONS, JSON_DUMP)
from .auth import AuthPhase, auth_required_message
from .error import Disconnect
from .messages import error_message
JSON_DUMP = partial(json.dumps, cls=JSONEncoder, allow_nan=False)
class WebsocketAPIView(HomeAssistantView):
"""View to serve a websockets endpoint."""
@@ -62,7 +57,10 @@ class WebSocketHandler:
break
self._logger.debug("Sending %s", message)
try:
await self.wsock.send_json(message, dumps=JSON_DUMP)
if isinstance(message, str):
await self.wsock.send_str(message)
else:
await self.wsock.send_json(message, dumps=JSON_DUMP)
except (ValueError, TypeError) as err:
self._logger.error('Unable to serialize to JSON: %s\n%s',
err, message)