Closed piekey1994 closed 1 year ago
I cannot reproduce this on main
, or even on 22.3
using uvicorn.
from sanic import Request, Sanic
app = Sanic("TestApp")
@app.websocket("/feed")
async def feed(request: Request, ws):
while True:
data = await ws.recv()
await ws.send(data)
I am closing this now as I see no bug. If you have a reproducible snippet, please send.
I cannot reproduce this on
main
, or even on22.3
using uvicorn.from sanic import Request, Sanic app = Sanic("TestApp") @app.websocket("/feed") async def feed(request: Request, ws): while True: data = await ws.recv() await ws.send(data)
I am closing this now as I see no bug. If you have a reproducible snippet, please send.
I started my sanic app with UvicornWorker. The original websocket will become WebsocketConnection. When I call
the ws.recv function will report an error if bytes data is received at this time.
KeyError:‘text’
https://github.com/sanic-org/sanic/blob/main/sanic/server/websockets/connection.py
` async def recv(self, *args, **kwargs) -> Optional[str]:
message = await self._receive()
if message["type"] == "websocket.receive":
return message["text"]
elif message["type"] == "websocket.disconnect":
pass
return None`
There is no data of bytes type processed here.
No response
No response
ASGI
ubuntu
22.3
No response
I cannot reproduce this on
main
, or even on22.3
using uvicorn.from sanic import Request, Sanic app = Sanic("TestApp") @app.websocket("/feed") async def feed(request: Request, ws): while True: data = await ws.recv() await ws.send(data)
I am closing this now as I see no bug. If you have a reproducible snippet, please send.
main.py
from sanic import Sanic
app = Sanic('ws')
@app.websocket('/feed')
async def feed(req,ws):
while True:
data =await ws.recv()
print(data)
if data is None:
break
if __name__ == '__main__':
import websockets
import asyncio
async def main():
async with websockets.connect('ws://127.0.0.1::8889/feed') as ws:
await ws.send('123')
await ws.send(bytes(123))
asyncio.get_event_loop().run_until_complete(main())
run: gunicorn main:app --bind 0.0.0.0:8889 --worker-class uvicorn.workers.UvicornWorker
Nope. Still works fine for me.
Nope. Still works fine for me.
That's weird. However, you can look at this code in sanic, and it does seem to have a problem. It seems to only handle text, right? connection.py
async def recv(self, *args, **kwargs) -> Optional[str]:
message = await self._receive()
if message["type"] == "websocket.receive":
return message["text"]
elif message["type"] == "websocket.disconnect":
pass
return None
Yeah, will go back and look at the spec to see exactly what it's supposed to be.
Is there an existing issue for this?
Describe the bug
I started my sanic app with UvicornWorker. The original websocket will become WebsocketConnection. When I call the ws.recv function will report an error if bytes data is received at this time.
KeyError:‘text’
https://github.com/sanic-org/sanic/blob/main/sanic/server/websockets/connection.py ` async def recv(self, *args, **kwargs) -> Optional[str]: message = await self._receive()There is no data of bytes type processed here.
Code snippet
No response
Expected Behavior
No response
How do you run Sanic?
ASGI
Operating System
ubuntu
Sanic Version
22.3
Additional context
No response