我正在研究 Django channel 和异步应用程序,我对它完全陌生。 我开始看到这方面的教程。当我运行 django 服务器时,它工作正常但是当我加载页面或尝试连接 websocket king 客户端以测试服务器时,它显示错误 ->
WebSocket HANDSHAKING /ws/game/roomname [127.0.0.1:51190]
Exception inside application: __call__() missing 1 required positional argument: 'send'
Traceback (most recent call last):
File "C:\Users\user\anaconda3\lib\site-packages\channels\staticfiles.py", line 44, in __call__
return await self.application(scope, receive, send)
File "C:\Users\user\anaconda3\lib\site-packages\channels\routing.py", line 71, in __call__
return await application(scope, receive, send)
File "C:\Users\user\anaconda3\lib\site-packages\channels\sessions.py", line 47, in __call__
return await self.inner(dict(scope, cookies=cookies), receive, send)
File "C:\Users\user\anaconda3\lib\site-packages\channels\sessions.py", line 263, in __call__
return await self.inner(wrapper.scope, receive, wrapper.send)
File "C:\Users\user\anaconda3\lib\site-packages\channels\auth.py", line 185, in __call__
return await super().__call__(scope, receive, send)
File "C:\Users\user\anaconda3\lib\site-packages\channels\middleware.py", line 26, in __call__
return await self.inner(scope, receive, send)
File "C:\Users\user\anaconda3\lib\site-packages\channels\routing.py", line 150, in __call__
return await application(
File "C:\Users\user\anaconda3\lib\site-packages\asgiref\compatibility.py", line 34, in new_application
return await instance(receive, send)
TypeError: __call__() missing 1 required positional argument: 'send'
WebSocket DISCONNECT /ws/game/roomname [127.0.0.1:51190]
我是新手,我无法解决问题。请帮忙。
我的项目asgi.py
import os
from channels.routing import ProtocolTypeRouter , URLRouter
from channels.auth import AuthMiddlewareStack
from home.consumers import GameRoom
from django.urls import path
from django.core.asgi import get_asgi_application
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'tictac.settings')
application = get_asgi_application()
ws_pattern = [
path("ws/game/<room_code>" , GameRoom)
]
application = ProtocolTypeRouter({
"websocket" : AuthMiddlewareStack(URLRouter(
ws_pattern
))
})
如果可能,请对此提供详细解释,以便我了解错误所在。
最佳答案
改变你的
ws_pattern = [
path("ws/game/<room_code>" , GameRoom)
]
与
ws_pattern = [
path("ws/game/<room_code>" , GameRoom.as_asgi())
]
关于python - Django channel 错误 : TypeError: __call__() missing 1 required positional argument: 'send' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71375699/