Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make WebsocketImplProtocol async iterable #2490

Merged
merged 12 commits into from
Sep 20, 2022
12 changes: 11 additions & 1 deletion sanic/server/websockets/impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import random
import struct

from contextlib import suppress
from typing import (
AsyncIterator,
Dict,
Expand All @@ -13,7 +14,11 @@
)

from websockets.connection import CLOSED, CLOSING, OPEN, Event
from websockets.exceptions import ConnectionClosed, ConnectionClosedError
from websockets.exceptions import (
ConnectionClosed,
ConnectionClosedError,
ConnectionClosedOK,
)
from websockets.frames import Frame, Opcode
from websockets.server import ServerConnection
from websockets.typing import Data
Expand Down Expand Up @@ -840,3 +845,8 @@ def connection_lost(self, exc):
self.abort_pings()
if self.connection_lost_waiter:
self.connection_lost_waiter.set_result(None)

async def __aiter__(self):
with suppress(ConnectionClosedOK):
while True:
yield await self.recv()
ChihweiLHBird marked this conversation as resolved.
Show resolved Hide resolved