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

ASGI websocket recv text or bytes #2640

Merged
merged 1 commit into from Dec 25, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 12 additions & 4 deletions sanic/server/websockets/connection.py
Expand Up @@ -9,8 +9,10 @@
Union,
)

from sanic.exceptions import InvalidUsage

ASIMessage = MutableMapping[str, Any]

ASGIMessage = MutableMapping[str, Any]


class WebSocketConnection:
Expand All @@ -25,8 +27,8 @@ class WebSocketConnection:

def __init__(
self,
send: Callable[[ASIMessage], Awaitable[None]],
receive: Callable[[], Awaitable[ASIMessage]],
send: Callable[[ASGIMessage], Awaitable[None]],
receive: Callable[[], Awaitable[ASGIMessage]],
subprotocols: Optional[List[str]] = None,
) -> None:
self._send = send
Expand All @@ -47,7 +49,13 @@ async def recv(self, *args, **kwargs) -> Optional[str]:
message = await self._receive()

if message["type"] == "websocket.receive":
return message["text"]
try:
return message["text"]
except KeyError:
try:
return message["bytes"].decode()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't bytes be received as is, without decoding? This is what the built-in server does, to support binary data.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to receive it for now without breaking the existing contract. I think we need to revisit this.

Copy link
Member

@Tronic Tronic Dec 29, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, we need to. I would think this being necessary for 23.12.1 release because we cannot have LTS processing it wrong like that. Apparently not many use ASGI mode with binary WebSockets because this wasn't caught earlier.

The binary format is specifically meant for binary data, for which my own use cases include MsgPack, audio streaming and other raw data that will never be able to be decoded into text (if not for a freak accident of chance that still breaks the software). Never JSON which can be sent in text mode instead.

Applications can also employ the two different types on the same socket to differentiate raw data stream (binary) of control commands (text).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, probably sooner than a year from now and back port it.

except KeyError:
raise InvalidUsage("Bad ASGI message received")
elif message["type"] == "websocket.disconnect":
pass

Expand Down