python - Django channel 错误 : TypeError: __call__() missing 1 required positional argument: 'send'

标签 python django websocket channels

我正在研究 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/

相关文章:

python - 大型 celery 任务内存泄漏

javascript - Socket.IO中的跨域连接

javascript - NodeJS 和 Web 套接字 : Check if socket origin is the same as the web socket server

php - 如何在 linux 主机中运行 php 套接字服务器

python - 使用 python beautiful soup 和 requests 包时 HTML 内容不正确

python - 如何解压缩/解密 gzip 文件的单行

django - 如何解决 DRF 路由器重复的 url

Django:获取外键字段的值而不需要额外的 SQL 到 DB

python - 在 VirtualBox 中的 guest 操作系统上运行程序

python - 使用 Numpy for Python 创建 cx_Freeze exe