mirror of
https://github.com/home-assistant/core.git
synced 2025-07-29 16:17:20 +00:00
Fix ingress websocket forward not closing the websocket
Becuase the async iterator was used, it would stop iteration as soon as a close message was seen. This meant we would never send close to the other side
This commit is contained in:
parent
10b9e3b29c
commit
98601da3ea
@ -284,23 +284,32 @@ def _is_websocket(request: web.Request) -> bool:
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
|
_CLOSE_TYPES = {
|
||||||
|
aiohttp.WSMsgType.CLOSE,
|
||||||
|
aiohttp.WSMsgType.CLOSING,
|
||||||
|
aiohttp.WSMsgType.CLOSED,
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
async def _websocket_forward(
|
async def _websocket_forward(
|
||||||
ws_from: web.WebSocketResponse | ClientWebSocketResponse,
|
ws_from: web.WebSocketResponse | ClientWebSocketResponse,
|
||||||
ws_to: web.WebSocketResponse | ClientWebSocketResponse,
|
ws_to: web.WebSocketResponse | ClientWebSocketResponse,
|
||||||
) -> None:
|
) -> None:
|
||||||
"""Handle websocket message directly."""
|
"""Handle websocket message directly."""
|
||||||
try:
|
try:
|
||||||
async for msg in ws_from:
|
while msg := await ws_from.receive():
|
||||||
if msg.type is aiohttp.WSMsgType.TEXT:
|
msg_type = msg.type
|
||||||
|
if msg_type is aiohttp.WSMsgType.TEXT:
|
||||||
await ws_to.send_str(msg.data)
|
await ws_to.send_str(msg.data)
|
||||||
elif msg.type is aiohttp.WSMsgType.BINARY:
|
elif msg_type is aiohttp.WSMsgType.BINARY:
|
||||||
await ws_to.send_bytes(msg.data)
|
await ws_to.send_bytes(msg.data)
|
||||||
elif msg.type is aiohttp.WSMsgType.PING:
|
elif msg_type is aiohttp.WSMsgType.PING:
|
||||||
await ws_to.ping()
|
await ws_to.ping()
|
||||||
elif msg.type is aiohttp.WSMsgType.PONG:
|
elif msg_type is aiohttp.WSMsgType.PONG:
|
||||||
await ws_to.pong()
|
await ws_to.pong()
|
||||||
elif ws_to.closed:
|
elif msg_type in _CLOSE_TYPES:
|
||||||
await ws_to.close(code=ws_to.close_code, message=msg.extra) # type: ignore[arg-type]
|
await ws_to.close(code=ws_to.close_code, message=msg.extra) # type: ignore[arg-type]
|
||||||
|
break
|
||||||
except RuntimeError:
|
except RuntimeError:
|
||||||
_LOGGER.debug("Ingress Websocket runtime error")
|
_LOGGER.debug("Ingress Websocket runtime error")
|
||||||
except ConnectionResetError:
|
except ConnectionResetError:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user